36

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Embed Size (px)

Citation preview

Page 1: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general
Page 2: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

CIS_IS20_CSLO 1.  Explain computer programming concepts

CSLO1.6. Explain the purpose of general functionality provided by conditional statements/control structures, relational operators, and logical operators

Page 3: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multiway Branches

Page 4: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multiway Branches

A branching mechanism selects one out of a number of alternative actions The if-else-statement is a branching

mechanism Branching mechanisms can be a subpart of

another branching mechanism An if-else-statement can include another

if-else-statement as a subpart

Slide 3- 4

Page 5: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Nested Statements

A statement that is a subpart of another statementis a nested statement When writing nested statements it is normal to

indent each level of nesting

Example: if (count < 10) if ( x < y)

cout << x << " is less than " << y; else

cout << y << " is less than " << x;

Slide 3- 5

indented

Display 3.3

Page 6: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Nested if-else Statements

Use care in nesting if-else-statements Example: To design an if-else statement to

warn a driver when fuel is low, but tells the driver to bypass pit stops if the fuel is close to full. Other wise there should be no output.

Pseudocode: if fuel gauge is below ¾ then: if fuel gauge is below ¼ then: issue a warning otherwise (gauge > ¾) then: output a statement saying don't stop

Slide 3- 6

Page 7: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

First Try Nested if's

Translating the previous pseudocode to C++ could yield (if we are not careful)

if (fuel_gauge_reading < 0.75) if (fuel_gauge_reading < 0.25) cout << "Fuel very low. Caution!\n";

else cout << "Fuel over 3/4. Don't stop now!\n"; This would compile and run, but does not produce the

desired results The compiler pairs the "else" with the nearest previous

"if"

Slide 3- 7

Page 8: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Braces and Nested Statements

Braces in nested statements are like parenthesis in arithmetic expressions Braces tell the compiler how to group things

Use braces around substatements demonstrates the use of braces in

nested if-else-statements

Slide 3- 8

Display 3.4

Page 9: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multi-way if-else-statements

An if-else-statement is a two-way branch Three or four (or more) way branches can be

designed using nested if-else-statements Example: The number guessing game with

the number stored in variable number, the guess in variable

guess. How do we give hints?

Slide 3- 9

Page 10: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Number Guessing

The following nested statements implement the hints for our number guessing game if (guess> number)

cout << "Too high.";else if (guess < number) cout << "Too low."); else if (guess == number) cout << "Correct!";

Slide 3- 10

Page 11: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Indenting Nested if-else

Notice how the code on the previous slide creptacross the page leaving less and less space Use this alternative for indenting several nested

if-else-statements:if (guess> number)

cout << "Too high.";else if (guess < number)

cout << "Too low.");else if (guess == number)

cout << "Correct!";

Slide 3- 11

Page 12: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The Final if-else-statement

When the conditions tested in an if-else-statementare mutually exclusive, the final if-else can sometimes be omitted. The previous example can be written as if (guess> number)

cout << "Too high.";else if (guess < number)

cout << "Too low.");else // (guess == number)

cout << "Correct!";

Slide 3- 12

Page 13: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Nested if-else Syntax

A Multiway if-else statement is written as if(Boolean_Expression_1)

Statement_1else if ( Boolean_Expression_2) Statement_2 … else if (Boolean_Expression_n) Statement _nelse Statement_For_All_Other_Possibilities

Slide 3- 13

Page 14: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Program Example: State Income Tax

Write a program for a state that computes tax according to the rate schedule:

No tax on first $15,000 of income

5% tax on each dollar from $15,001 to $25,000

10% tax on each dollar over $25,000

Slide 3- 14

Display 3.5 (1)

Display 3.5 (2)

Page 15: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Refining if-else-statements

Notice that the line else if (( net_income > 15000 && net_income < = 25000))

can be replaced with else if (net_income <= 25000)

The computer will not get to this line unless it is already determined that net_income > 15000

Slide 3- 15

Page 16: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The switch-statement

The switch-statement is an alternative for constructing multi-way branches The example in Display 3.6 determines output

based on a letter grade Grades 'A', 'B', and 'C' each have a branch Grades 'D' and 'F' use the same branch If an invalid grade is entered, a default branch is

used

Slide 3- 16

Display 3.6 (1)Display 3.6 (2)

Page 17: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

switch Structures

switch structure: alternate to if-else

switch (integral) expression is evaluated first

Value of the expression determines which corresponding action is taken

Expression is sometimes called the selector

17

Page 18: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 18

switch Structures (cont’d.)

Page 19: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

switch Structures (cont’d.)

One or more statements may follow a case label

Braces are not needed to turn multiple statements into a single compound statement

When a case value is matched, all statements after it execute until a break is encountered

The break statement may or may not appear after each statement

switch, case, break, and default are reserved words

19

Page 20: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 20

switch Structures (cont’d.)

Page 21: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

switch-statement Syntax

switch (controlling expression){ case Constant_1: statement_Sequence_1

break; case Constant_2: Statement_Sequence_2 break; . . . case Constant_n: Statement_Sequence_n break; default: Default_Statement_Sequence}

Slide 3- 21

Page 22: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The Controlling Statement

A switch statement's controlling statement must return one of these types A bool value An enum constant An integer type A character

The value returned is compared to the constant values after each "case" When a match is found, the code for that case is used

Slide 3- 22

Page 23: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The break Statement

The break statement ends the switch-statement Omitting the break statement will cause the code

for the next case to be executed! Omitting a break statement allows the use of

multiple case labels for a section of code case 'A':

case 'a': cout << "Excellent."; break;

Runs the same code for either 'A' or 'a'

Slide 3- 23

Page 24: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The default Statement

If no case label has a constant that matches the controlling expression, the statements followingthe default label are executed If there is no default label, nothing happens

when the switch statement is executed It is a good idea to include a default section

Slide 3- 24

Page 25: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Switch-statements and Menus

Nested if-else statements are more versatile thana switch statement

Switch-statements can make some code more clear A menu is a natural application for a switch-

statement

Slide 3- 25

Display 3.7 (1)

Display 3.7 (2)

Page 26: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Chapter 3 -- End

Slide 3- 26

Page 27: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.3

Slide 3- 27

Back Next

Page 28: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.4

Slide 3- 28

Back Next

Page 29: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.5 (1/2)

Slide 3- 29

Back Next

Page 30: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.5(2/2)

Slide 3- 30

Back Next

Page 31: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.6 (1/2)

Slide 3- 31

Back Next

Page 32: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.6 (2/2)

Slide 3- 32

Back Next

Page 33: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.7 (1/2)

Slide 3- 33

Back Next

Page 34: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.7 (2/2)

Slide 3- 34

Back Next

Page 35: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.8 (1/2)

Slide 3- 35

NextBack

Page 36: Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Display 3.8 (2/2)

Slide 3- 36

Back Next