nested conditional statements. a conditional statement (i.e., an if-statement or an if-else-...

23
Nested conditional statements

Upload: dorothy-chase

Post on 23-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Nested conditional statements

Page 2: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Nested conditional statements

• A conditional statement (i.e., an if-statement or an if-else-statement) is also a statement

• We can use an if-statement or an if-else-statement in the then-part (and in the else-part) of a conditional statement !!!     

• Nested conditional statement = a conditional statement where

the then-part and/or the else-part contains another conditional statement

Page 3: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: determine the price for a hair cut (1)

Page 4: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: determine the price for a hair cut (2)

Page 5: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: determine the price for a hair cut (3)

Page 6: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: assign a letter grade to a number grade (1)

Page 7: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: assign a letter grade to a number grade (2)

Page 8: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: assign a letter grade to a number grade (3)

Page 9: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: assign a letter grade to a number grade (4)

Page 10: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: assign a letter grade to a number grade (5)

Page 11: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

Programming example: assign a letter grade to a number grade (6)

Page 12: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

A three-way selection construct (1)

• A two-way selection is a choice between 2 mutually exclusive cases:

Page 13: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

A three-way selection construct (2)

• A three-way selection is a choice between 3 mutually exclusive cases:

Page 14: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

A three-way selection construct (3)

• A common way to ensure that the 3 choices are mutually exclusive is as follows:

Page 15: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

A three-way selection construct (4)

Page 16: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

A three-way selection construct (5)

Page 17: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The N-way selection construct (1)

• A N-way selection construct looks like the following:

Page 18: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The N-way selection construct (2)

Page 19: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The switch statement: an N-way selection statement (1)

• an intrinsically N-way selection statement: the switch statement

Page 20: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The switch statement: an N-way selection statement (2)

•  INT-EXPRESSION must be an integer valued expression

• The result of the INT-EXPRESSION is compared to the integer values given in the case clauses

• The statements following the case clause up to the break 

statement are executed

• If the result of the INT-EXPRESSION is not equal to any INT-VALUE in the case clauses, then the statements in the default case are executed 

Page 21: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The switch statement: an N-way selection statement (3)

Page 22: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The switch statement: an N-way selection statement (4)

• The following types are automatically converted into an integer typed value in expressions:

Page 23: Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement

The switch statement: an N-way selection statement (5)