Nested

advertisement
Nested conditional statements
Nested conditional statements
• A conditional statement (i.e., an if-statement or an if-elsestatement) is also a statement
• We can use an if-statement or an if-else-statement in the thenpart (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
Programming example: determine
the price for a hair cut (1)
Programming example: determine
the price for a hair cut (2)
Programming example: determine
the price for a hair cut (3)
Programming example: assign a
letter grade to a number grade (1)
Programming example: assign a
letter grade to a number grade (2)
Programming example: assign a
letter grade to a number grade (3)
Programming example: assign a
letter grade to a number grade (4)
Programming example: assign a
letter grade to a number grade (5)
Programming example: assign a
letter grade to a number grade (6)
A three-way selection construct (1)
• A two-way selection is a choice between 2 mutually
exclusive cases:
A three-way selection construct (2)
• A three-way selection is a choice between 3 mutually
exclusive cases:
A three-way selection construct (3)
• A common way to ensure that the 3 choices are mutually
exclusive is as follows:
A three-way selection construct (4)
A three-way selection construct (5)
The N-way selection construct (1)
• A N-way selection construct looks like the following:
The N-way selection construct (2)
The switch statement: an Nway selection statement (1)
• an intrinsically N-way selection statement: the switch statement
The switch statement: an Nway 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
The switch statement: an Nway selection statement (3)
The switch statement: an Nway selection statement (4)
• The following types are automatically converted into
an integer typed value in expressions:
The switch statement: an Nway selection statement (5)
Download