Beruflich Dokumente
Kultur Dokumente
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
9 10
13 14
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
#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
25 26
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
37 38
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
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