Sie sind auf Seite 1von 6

Chapter 3(b) - Selection Nested if statement

 Nested if statement  We may also include another if statement in the if


structure:
 Multiple if/else statement if (expression)
(else if construct) {
if (expression)
 Switch statement a;
 Conditional expression Operator else
statement b;
}
else
statement c;
1 2

Nested if statement
 An if statement may appear inside another if
statement.
 When this is done, the inner if statement is
said to be “nested” inside the outer if statement.
 For example, you are eligible to vote only if you
are a citizen and you are at least 18 years old
 If the user’s input is that they are at least 18
years old and a citizen, the program outputs
that they are eligible to vote
3 4

Sample run
Enter your age: 18
Are you a citizen (Y/N): Y
You are eligible to vote
===
Enter your age: 18
Are you a citizen (Y/N): N
You are not eligible to vote
===
Enter your age: 17
Are you a citizen (Y/N): Y
You are not eligible to vote
===
Enter your age: 17
Are you a citizen (Y/N): N
You are not eligible to vote 5 6

 The if/else structure comparing whether  The following program is an application at


the user is a citizen is nested within another the movie admission counter to determine
if/else structure comparing whether the the age of the user in order to decide
user is at least 18 years old. whether they are eligible for free entrance
 The if/else structure comparing whether  If the user’s input indicates that they are
the user is a citizen is referred to as the either no more than 12 years old or at least
“inner” if/else structure. 65 years old, the program outputs that their
 The if/else structure comparing whether admission is free.
the user is at least 18 years old is referred  Otherwise, the program outputs that they
to as the “outer” if/else structure. have to pay.
7 8
Sample run
 Enter your age: 12
Admission is free
===
 Enter your age: 13
You have to pay
===
 Enter your age: 65
Admission is free

9 10

 The inner if/else structure, comparing whether


the user is at least 65 years old, is nested
within the outer if/else structure, comparing
whether the user is over 12 years old.
 By this nesting, the comparison of whether the
user is at least 65 years old is made only if the
user is over 12 years old.
 Alternatively, the program could have been
written using the multiple if/else structure in
place of the nested if statements
11 12

 Nesting one if statement inside another by its


very nature may be somewhat difficult to write
and understand
 However, the multiple if/else structure has the
disadvantage of repeating the same cout
statement for both the if and else if parts.
 While this is just one line of repetitive code in
this program, in more complex programs the
repetitive code could be many lines long.

13 14

 The next example uses nested if else


statement to find the minimum of 3
numbers
 This program has the same effect as the
one in Example 4 and Example 9
 It is more efficient than Example 9 because
on any run it will evaluate only two simple
conditions instead of three compound
conditions
15 16
Multiple if-else statement

 There exist situations with more than  Use multiple if / else statement when
two mutually exclusive alternatives there are three or more mutually
 For example, if you take a test, your exclusive alternatives
grade may be one of five types: A, B,  The multiple if / else statement has an
C, D, or F.
if part and an else part, like an if/else
 Additionally, these grades are mutually statement.
exclusive; you can’t get an A and a C
on the same test.  However, it also has one or more
else if parts (else if construct)
17 18

The else if Construct

 The else if part works similarly to an Syntax:


if expression.
 The else if keywords are followed by
a relational expression If (condition) statement1;
 If the expression is true, then the else if (condition) statement2;
conditional statement or statements else if (condition) statement3;
“belonging” to the else if part is else if (condition) statement4;
executed.
else statement5;
19 20

// Using the else if construct to select a range of score

#include <iostream>
using namespace std;
int main()
If (condition) {
statement1; int score;
else if (condition)
cout << "Enter your test score: ";
statement2;
cin >> score;
else if (condition)
if (score >= 90) cout << "Your grade is an A.\n";
statement3;
else if (score >= 80) cout << "Your grade is a B.\n";
else if (condition)
statement4; else if (score >= 70) cout << "Your grade is a C.\n";
else else if (score >= 60) cout << "Your grade is a D.\n";
statement5; else cout << "Your grade is an F.\n";
}
21 22

Sample Run

Enter your test score: 45


Your grade is a D.

Enter your test score: 80


Your grade is a B.

Enter your test score: 105


Error: that score is out of range
23 24
 Next example covers all other
possibilities better in contrast to
previous example
 It considers the exam score which is
out of normal range (i.e. 0-100)

25 26

The switch Statement


Syntax:
 You cannot have an else if part without a
preceding if part switch (expression)
{
 However, you may have an if part and one
case constant1: statementList1; break;
or more else if parts without an else part
case constant2: statementList2; break;
 The downside in omitting the else part is case constant3: statementList3; break;
you will not have code to cover the “none of :
the above” scenario in which none of the case constantN: statementListN; break;
relational expressions belonging to the if default: statementList0;
part and else if parts is true }
27 28

 The switch statement is similar to a multiple  This evaluates the expression and then its
if /else statement. value among case constants
 Expression must evaluate to an integral type  If the value is found among the constants
 The constants must be integral constants listed, then the statements in the
 Break statements can be optional but corresponding stated statementList are
without it, the program execution will not executed
branch directly out of the switch block after
it finishes executing its case statement  Otherwise if there is default (optional), then
sequence the program branches to its statementList

29 30

31 32
switch versus multiple
if/else
 The comparison following the if part  By contrast, in a switch statement, the constant
integer expression following a case keyword
may be independent of the
must be compared with the value following the
comparison following an else if part switch keyword, and nothing else
 Eg.  In a switch statement (once a matching case
if (apples == oranges) statement is found), unless a break statement
is reached, execution “falls through” to the
do this; following case statements that execute their
else if (sales >= 5000) conditional statements without checking for a
match
do that; 33 34

Necessity of falls through


 For example, if you removed the break  In the following modification of the
statements from the program, you could
have the following sample run:
grade program, the falling-through
behavior permits the user to enter a
Enter your grade: A lowercase grade in addition to an
Your average must be between 90 – 100 uppercase grade
Your average must be between 80 - 89
Your average must be between 70 - 79
Your average must be between 60 - 69
Your average must be below 60 35 36

37 38

Problem of falls through

39 40
 Another difference between switch and else if  By contrast, a case statement cannot be followed
statements concerns the handling of ranges of by an expression (e.g. testScore >= 90) because
numbers the case statement keyword has to be followed by
 When it comes to range of numbers, the switch an integer constant
statement appears to be more cumbersome
 Instead, a case statement would be necessary for
 This is because the switch statement keyword has each possible test score.
to be followed by an integer constant
 For example, earlier in this chapter we used an else  The following code fragment shows only the code
if statement to output the user’s grade based on for an A or B grade to avoid the code example
the test score that was input by the user. The being unduly long, but the code for a C or D grade
issued grade was an A if the test score was would be essentially a repeat (an F grade would be
between 90 and 100, a B if the test score was handled with the default keyword)
between 80 and 89, and so on.
41 42

This code example illustrates that the switch statement


is more cumbersome than the if /else if /else structure in
dealing with ranges of numbers.

43 44

The conditional
expression Operator
 C++ provides a special operator that
often can be used in pace of the
if….else statement
 Should be used sparingly
 Use only when the condition and both
expression are very simple
 Syntax:
condition ? expression1 : expression2
45 46

Das könnte Ihnen auch gefallen