Title: Chapter 3 More Flow of Control
1Chapter 3More Flow of Control
Goals
- To analyze the use of Boolean expressions
- To introduce the notion of enumerated types
- To explore the switch statement as an
alternative to multiway if-else statements
- To view the conditional statement as a
alternative to a simple if-else statement
- To examine the for-statement as a looping option
- To demonstrate the design of good nested loops
2Precedence Rules for Boolean Expressions
With the addition of Boolean operators, the
precedence rules that C uses to evaluate
expressions become more complex.
- Parentheses still take the highest precedence
- Multiplication, division, and modulus come
second.
- Addition and subtraction take the next
precedence.
- Order-related inequality operators (lt, gt, lt,
gt) are next.
- The pure equality/inequality (, !) operators
are next.
- The Boolean AND operator () takes the next
precedence.
- The Boolean OR operator () takes the lowest
precedence.
- Precedence ties are still handled in
left-to-right fashion.
3Precedence Rules Examples
int x 7, y 7, z 7 if (x y z) cout
ltlt YES else cout ltlt NO
int a 5, b 4, c 3 if (a lt b lt c) cout
ltlt YES else cout ltlt NO
Output NO The left equality is checked and
evaluates to true (numerical 1), which is not
equal to the z-value!
Output YES The left inequality is checked and
evaluates to false (numerical 0), which is less
than the c-value!
4Boolean Expressions -Short-Circuit Evaluation of
When a Boolean expression using the operator
is evaluated, the first subexpression is
evaluated first. if it evaluates to false, then
the second subexpression is ignored.
When this code segment is encountered and xs
value is zero, z will be assigned either the
value 100 or the value -1.
if ((x ! 0) (y/x gt 1)) z 100 else z
-1
When this code segment is encountered and xs
value is zero, the program crashes due to the
attempt to divide by zero!
if ((y/x gt 1) (x ! 0)) z 100 else z
-1
5Boolean Expressions -Short-Circuit Evaluation of
When a Boolean expression using the operator
is evaluated, the first subexpression is
evaluated first. if it evaluates to true, then
the second subexpression is ignored.
When this code segment is encountered and cts
value is zero, the message is output.
if ((ct 0) (total/ct gt 70)) cout ltlt NO
PROBLEMO
When this code segment is encountered and cts
value is zero, the program crashes due to the
attempt to divide by zero!
if ((total/ct gt 70) (ct 0)) cout ltlt NO
PROBLEMO
6Enumerated Types
To enhance the readability of ones code, a
programmer can define an enumerated type, which
consists of a set of integer constants.
include ltiostreamgt using namespace std enum
MonthNumber JANUARY 1, FEBRUARY, MARCH,
APRIL, MAY, JUNE, JULY,
AUGUST, SEPTEMBER, OCTOBER, NOVEMBER,
DECEMBER void main() MonthNumber
monthCount JANUARY double monthlyRate
12.34, totalCost 0.0 cout.setf(iosfixed)
cout.setf(iosshowpoint)
cout.precision(2) cout ltlt "Computing Annual
Costs\n\n" while (monthCount lt DECEMBER)
totalCost monthlyRate
monthCount MonthNumber(monthCount 1)
cout ltlt endl ltlt endl ltlt "Total " ltlt totalCost
ltlt endl ltlt endl return
7switch Statements
C switch statements provide a simple
alternative to the use of convoluted nested
if-else statements.
if (month 2) if (year 4 0)
daysInMonth 29 else daysInMonth
28 else if ((month 4) (month
6) (month 9) (month
11)) daysInMonth 30 else daysInMonth
31
switch (month) case 2 if
(year 4 0) daysInMonth
29 else daysInMonth
28 break case 4
case 6 case 9 case 11 daysInMonth 30
break default daysInMonth 31 break
8include ltiostreamgt using namespace std void
main() char letter bool gotAVowel
false do cout ltlt "Please enter a
letter " cin gtgt letter switch(letter) cas
e 'a' case 'A' cout ltlt "Apple\n" gotAVowel
true break case 'e' case 'E' cout ltlt
"Egg\n" gotAVowel true break case 'i'
case 'I' cout ltlt "Iodine\n" gotAVowel true
break case 'o' case 'O' cout ltlt
"Oval\n" gotAVowel true break case 'u'
case 'U' cout ltlt "Upper\n" gotAVowel true
break default cout ltlt "That is not a vowel.
Please try again.\n" while(!gotAVowel)
The break statements ensure that the switch
statement is exited once the appropriate case is
handled. Without the break statements, all cases
might execute.
EXAMPLE
The default case is executed if no other case
value matches.
9Conditional Statements
C conditional statements provide a concise
alternative to the use of relatively simple
if-else statements.
if (val gt 0) cout ltlt GOOD else cout ltlt
BAD
(val gt 0) ? (cout ltlt GOOD) (cout ltlt BAD)
if (year 4 0) daysInMonth 29 else
daysInMonth 28
daysInMonth (year 4 0) ? 29 28
10The for Statement
The C for statement is designed to facilitate
looping when the control of the loop contains
three standard features
- An initialization action that is performed just
before the loop is started the first time.
- A Boolean expression that is checked just before
entering the loop at each iteration.
- An update action that is performed just after
each iteration of the loop is completed.
for (initAction booleanExp updateAction)
bodyOfLoop
11A for-loop Example
include ltiostreamgt using namespace std const
double PAY_RATE 10.15 void main() double
payForWeek 0.0 int dayOfWeek double
hoursWorked for(dayOfWeek 1 dayOfWeek
lt 7 dayOfWeek) cout ltlt "How many
hours did you work on day " ltlt dayOfWeek ltlt "?
" cin gtgt hoursWorked (dayOfWeek
1) ? (payForWeek 1.5 hoursWorked
PAY_RATE) (payForWeek hoursWorked
PAY_RATE) cout.setf(iosfixed)
cout.setf(iosshowpoint) cout.precision(2)
cout ltlt endl ltlt endl ltlt "Your Pay For \n"
ltlt "The Week Is " ltlt payForWeek ltlt
endl ltlt endl
This inequality is checked just before the body
of the loop is entered (or re-entered).
dayOfWeek is initialized to 1when the for-loop is
first encountered.
This increment statement is executed right after
each execution of the body of the loop.
12Nested for-loops
include ltiostreamgt using namespace std void
main() int iterationNbr, repetitionNbr,
indentSpaceNbr for (iterationNbr 1
iterationNbr lt 25 iterationNbr) for
(repetitionNbr 1 repetitionNbr lt 1000
repetitionNbr) cout ltlt '\r'
for (indentSpaceNbr 1 indentSpaceNbr lt
iterationNbr indentSpaceNbr) cout
ltlt ' ' cout ltlt "HAPPY NEW YEAR!"
cout ltlt endl ltlt endl return