Selection
• In C++, there are two selections or branch
control structures:
1- if statement.
requires evaluation of logical exp
one way, two way, or multiple (nested if)
selection
2- switch structure.
does not require evaluation.
1
One-Way Selection
• The syntax of one-way selection is:
• The statement is executed if the value of the
expression is true
• The statement is bypassed if the value is
false; program goes to the next statement
• if is a reserved word
2
One-Way Selection (continued)
3
One-Way Selection (continued)
4
5
One-Way Selection (continued)
6
Two-Way Selection
• Two-way selection takes the form:
• If expression is true, statement1 is
executed; otherwise, statement2 is
executed
• else is a reserved word
7
Two-Way Selection (continued)
8
Two-Way Selection (continued)
9
Two-Way Selection (continued)
10
Compound (Block of) Statement
• If and if …else control only one
statement.
• If you want to execute more statements , you
need to use braces.
• Compound statement (block of statements):
11
Compound (Block of) Statement
(continued)
if (age > 18)
{
cout << "Eligible to vote." << endl;
cout << "No longer a minor." << endl;
}
else
{
cout << "Not eligible to vote." << endl;
cout << "Still a minor." << endl;
}
12
Multiple Selections: Nested if
• Nesting: one control statement in another
• An else is associated with the most recent
if that has not been paired with an else
• Using else statement rather than multiple if
make execution faster because if exp is
evaluated to true, it skips the rest of
statements, as shown in next examples
13
14
15
Comparing if…else Statements
with a Series of if Statements
16
int x=7;
if (x>=3 && x<10)
x+=3;
else if (x==5)
x=7;
cout<<x;
17
int x=7, y=11;
if (x%y ==5)
if (y%11 == 0)
x+=2;
else
x-=2;
y=y+2;
cout<<x<<endl<<y<<endl;
18
Confusion Between == and =
• C++ allows you to use any expression that
can be evaluated to either true or false as
an expression in the if statement:
if (x = 5)
cout << "The value is five." << endl;
• The appearance of assignment operator (=)
in place of equality operator (==) resembles a
silent killer
− It is not a syntax error
− It is a logical error (typographical error).
19
Conditional Operator (?:)
• Conditional operator (?:) takes three arguments
− Ternary operator
• Syntax :
expression1 ? expression2 : expression3
• If expression1 is true, the result of the conditional
expression is expression2
− Otherwise, the result is expression3
• if (a >= b)
max = a; max = (a >= b) ? a : b;
else
max = b;
20
Exemple 1
int x=4;
x==5 ? cout<<x : cout<<++x;
Exemple 2
int x=3;
x==3 ? (x=6, cout<<x) :( x=5 ,cout<<x);
21
Exemple 3:
int x=10 , y=2;
x+= (x<3 ? y+=14 : 'A');
cout<<x<<endl<<y<<endl;
22
Example 4:
int x;
cin>>x;
cout<<(x>=50) ? "pass" : "fail";
Example 5:
int x;
cin>>x;
cout<<((x>=50) ? "pass" : "fail");
23
switch Structures
• switch structure: alternate to
if-else
• switch (expression) expression
is evaluated first
• Value of the expression determines
which corresponding action is
taken
• Expression is sometimes called the
selector, and it’s value can be only
of type integral (char, int, bool).
24
25
switch Structures (continued)
• One or more statements may follow a case
label
• When value of Exp is matched against a case
value (label), the statements execute until
either a break is found or the end of switch.
• If it doesn’t match any of the case values,
statements following the default label
execute, if no default label and no matching,
the entire switch statement is skipped.
26
switch Structures (continued)
• Braces are not needed to turn multiple
statements into a single compound statement
• The break statement (causes immediate exit
from switch structure) may or may not appear
after each case.
• Program executes all statements until a break
is encountered, skipping all case labels in
between.
• switch, case, break, and default are
reserved words
27
28
29
Summary
• Control structures alter normal control flow
• Most common control structures are selection
and repetition
• Relational operators: ==, <, <=, >, >=, !=
• Logical expressions evaluate to 1 (true) or 0
(false)
• Logical operators: ! (not), && (and), || (or)
30
Summary (continued)
• Two selection structures: one-way selection
and two-way selection
• The expression in an if or if...else
structure is usually a logical expression
• No stand-alone else statement in C++
− Every else has a related if
• A sequence of statements enclosed between
braces, { and }, is called a compound
statement or block of statements
31
Summary (continued)
• Using assignment in place of the equality
operator creates a semantic error
• switch structure handles multiway selection
• break statement ends switch statement
32