1/16
CSCI 135
Control Flow (Selection)
2/16
Basic Language Constructs
Recall: each statement in an imperative language updates the
value of some variable.
⇒ Classes of Constructs:
Declaration of variables: How does the variable map onto
memory?
Updating variable: How do we update the variable, and what
do we update it with?
I/O: How do we input and output data into the system?
Control: How do we control which statement gets executed
next?
Modularity and Object Orientation: How do we organize the
program to enable proper software engineering practices?
Comments: Used to describe code; ignored by compiler, but
code unmaintainable without good comments!
C/C++: on line beginning with // or surrounded by /*, */
3/16
Types of Control
Selection: Execute a different instruction depending on the
result of evaluating some [Boolean] condition.
if/else, multiway if, switch, ? :
Iteration: Repetitively execute some instruction until some
condition is met,
for, while, do while
Goto: Execute some instruction (not necessarily the next one)
next
A Absolutely, NEVER never never use this (See Dijkstra,
“Go To Statement Considered Harmful”); leads to
unmaintainable spaghetti code.
Break: Leave block
4/16
Conditions
A condition is a Boolean expression - i.e., one that evaluates to
true or false.
Most commonly either a relational operator or a logical
combination of conditions:
Relational (Comparison) Operators: == != < > <= >=
(semantics vary by type of operands)
Logical operators: ! && ||
Note: conjuncts/disjuncts are evaluated left-to-right and only
if needed (called short-circuit evaluation)
See Fig. 2.3 for precedence order (but better to use parentheses)
BEquality (==) is not the same as assignment (=).
BRecent versions of C++ support alternate syntaxes for logical
operators (e.g., “and”). Avoid these as they are rarely used (and
not compatible with earlier C/C++ standards).
5/16
Conditions - Examples
n <= 100 && n >= 0
n < 101 && n >= 0
grade > ’c’ && grade <= ’f’
y < x && y >= 0
?© Which points of the Cartesian plane does this cover?
!s.empty() && (s[0] == ’f’ || s[0] == ’g’)
?© Why does the order of conjuncts matter?
6/16
Short-Circuit Evaluation
Almost all languages: left-to-right, completely
C/C++ Special Cases: &&, ||, ?
Evaluate the 2nd operand only if it can change the result of
the expression; i.e., short-circuit the evaluation. Ex:
Don’t evaluate Q in P && Q if P evaluates to false
Don’t evaluate Q in P || Q if P evaluates to true
Similarly for ?
⇒ useful when evaluating the Q would lead to an error for the
’wrong’ case of P
Ex: !s.empty() && (s[0] . . .)
7/16
Caveat: Conditions in C
A C condition actually evaluates to an integer (not a true-false
bool). If the integer is 0, it is interpreted as false; otherwise it is
interpreted as true.
Normally, you don’t need to worry about this (phew!). BUT
?© What would if (n=5) S1 else S2 do if n is initially 2?
7/16
Caveat: Conditions in C
A C condition actually evaluates to an integer (not a true-false
bool). If the integer is 0, it is interpreted as false; otherwise it is
interpreted as true.
Normally, you don’t need to worry about this (phew!). BUT
?© What would if (n=5) S1 else S2 do if n is initially 2?
1 Evaluate the condition n=5, which is actually an assignment
statement/expression that stores 5 in n and evaluates to the
rval (5)
2 Since 56=0, interpret it as true
3 Execute S1
BConfusing = and == is a very common source of bugs!
8/16
& vs &&
& Bit-wise and operator
&& Logical and operator, produces 1 iff both operands are true
(non-zero), and 0 otherwise.
Ex:
x = 1 ; y=2;
z = x & y ; evaluates to 0
z = x && y ; evaluates to 1
(similarly for | vs ||)
9/16
Selection: If/else
if S1 else S2
(commonly called if/then, though C doesn’t use keyword then)
Execute S1 if cond evaluates to true, and S2 otherwise, where
S1/S2 are statements.
(else S2 is optional)
Example:
i f ( h r s < 40)
pay = r a t e ∗ h r s ; indent block
e l s e { start of compound statement
pay = r a t e ∗40 + 1.5∗ r a t e ∗( hrs −40);
pay = pay − f t d e d ; // deduc t i on f o r f u l l t i m e r s
} ; end of compound statement
// December bonus f o r a l l
i f (month==12) pay=pay ∗ 1 . 1 ; no else case here
BGood practice (not practiced above): use {} even if block has
only one statement
10/16
Selection with Multiple Conditions
i f ( h r s < 10)
{ . . . }
e l s e
i f ( h r s < 15) // [ 1 0 , 1 4 ] hour s
{ . . . }
e l s e
i f ( h r s < 20) // [ 1 5 , 1 9 ] hour s
{ . . . }
e l s e
i f ( h r s < 30) // [ 2 0 , 2 9 ] hour s
{ . . . }
e l s e // >30 hours
{ . . . }
⇒ Indentation is a nightmare!
11/16
Multiway If-Else
Better solution:
i f ( h r s < 10)
{ . . . }
e l s e i f ( h r s < 15) // [ 1 0 , 1 4 ] hour s
{ . . . }
e l s e i f ( h r s < 20) // [ 1 5 , 1 9 ] hour s
{ . . . }
e l s e i f ( h r s < 30) // [ 2 0 , 2 9 ] hour s
{ . . . }
e l s e // >30 hours
{ . . . }
Not a new construct, just a different (better) indentation style
12/16
Selection with Many Cases
A statement for controlling multiple (> 2) branches
Condition must be based on some expression that evaluates to
an integral value (all types of int, char, some others)
Can do the same with if statements, but switch may be more
convenient and readable
Especially useful for ’menus’ (one case for each menu option)
13/16
Selection: Switch
switch ( e xp r ) {
case v a l 1 :
s tmt 1 executed if expr evaluates to val1
break ; exit [entire] switch statement
case v a l 2 :
s tmt 2
break ;
. . .
case v a l n
s tmt n
break ;
defau l t : executed if none of above cases applied
d e f a u l t s tm t
}
Expr must be integral
Without break, control goes through the next case (common error
to omit break, but sometimes useful)
14/16
Switch Example
char t e s t G r a d e ;
sw i t ch ( t e s t G r a d e ) {
case ’ a ’ :
cout << ” C o n g r a t u l a t i o n s ! ” ;
break ; prevents next message from being printed
case ’ b ’ :
cout << ” Jus t a l i t t l e more work needed ” ;
break ;
case ’ c ’ :
cout << ”Need to work h a r d e r ! ” ;
break ;
case ’ d ’ : no break; falls through to next case
case ’ f ’ :
cout << ”Not good ” ;
break ;
d e f a u l t : don’t forget the ’error’ case!
cout << ” E r r o r : u n r e c o g n i z e d grade ” ;
} ;
cout << e n d l ; after switch; done in all cases
15/16
Selection: Conditional
Shorthand for [typically] 1-line if-else
i f (m > n )
max = m;
e l s e
max = n ;
≡ max = (m > n ) ? m : n ;
Also called ternary operator
Good for quick one-liner, but can be misused:
?© What does this do? (a