Title: CONTROLLING PROGRAM FLOW
1CHAPTER 4
2The if Statement
- Allow a program to make decisions.
- Example
- if (Temp lt 5)
- cout ltlt Wear a coat today. ltlt endl
- Syntax
- if (condition)
- statement
3Boolean expression
- An expression that evaluates to either true or
false. - Use relational operators
- Operator Meaning
- equal
- lt less than
- lt less than or equal to
- gt greater than
- gt greater than equal to
- ! not equal to
4Examples
- ch lt Z
- Grade 12
- FruitName Banana
- Radius ! 0
- 2piRadius gt Area
5/Temperature program /include
ltiostream.hgtint main( ) double Temp cout
ltlt Enter todays temperature(Celsius) cin
gtgt Temp if (Temp lt 5) cout ltlt Wear a
coat today. ltlt endl cout ltlt Have a gret
day! ltlt endl return (0)
64.2 if Pitfalls
- Never make equality comparisons ( or !) with
values that may have fractional parts. - if (480 4.8 100)
- cout ltlt These are equal!
- Compare values of only the same type
- Example 2 gt 3 (page 7.12)
- vs
- Misplaced semicolon
74. 3 The if-else Statement
- Can include an else clause when condition is
false -
- if (condition)
- statement
- else
- statement
- Indention is good programming style.
8- if (Temp lt 5)
- cout ltlt Wear a coat today. ltlt endl
- else
- cout ltlt Dont wear a coat today. ltlt endl
9- Modify the Circle Area program to display an
error message when the value entered for Radius
is negative. If Radius is a positive value or
zero, only the resulting area in a message should
be displayed. The error message should look
similar to - Radius entered was -3.4
- Negative radii are illegal.
10- include ltiostream.hgt
- int main( )
-
- double Radius
- cout ltlt Enter a radius
- cin gtgt Radius
- if (Radius lt 0)
-
- cout ltlt Radius entered was ltlt Radius
- cout ltlt Negative radii are illegal. ltlt endl
-
- else
-
- cout ltlt Radius entered was ltlt Radius
- cout ltlt Area ltlt (3.14 Radius Radius)
ltlt endl -
- return (0)
114.5 Nested if Statements
- An if statement that contains another if
statement - if (Votes1 Votes2)
- cout ltlt It was a tie! ltlt endl
- else
- if (Votes1 gt Votes 2)
- cout ltlt Candidate1 ltlt is the winner
- else
- cout ltlt Candidate2 ltlt is the winner.
124.6 The else-if Ladder
- Used to decide among three, four, or more
actions. - if (temp lt 0)
- cout ltlt Freezing
- else if (temp lt 13)
- cout ltlt Cool
- else if (temp lt 41)
- cout ltlt Hot
- else
- cout ltlt Very hot
Does not contain an if statement. It is executed
of all other conditions are false. DEFAULT.
134.8 Logical Operators
- and represented by
- (true) (true) evaluates to true
- (true) (false) evaluates to false
- (false) (true) evaluates to false
- (false) (false) evaluates to false
- if ((Temp lt 5) (Wind gt 24))
- cout ltlt Wear a coat today!
144.8 Logical Operators
- or represented by
- (true) (true) evaluates to true
- (true) (false) evaluates to true
- (false) (true) evaluates to true
- (false) (false) evaluates to false
- if ((Temp lt 5) (Wind gt 24))
- cout ltlt Wear a coat today!
154.8 Logical Operators
- not represented by !
- !(true) evaluates to false
- !(false) evaluates to true
- if (!(Temp lt 5))
- cout ltlt DONT wear a coat today!
164.9 Looping The do-while Statement
- iteration
- Repeat one or more statements during program
execution. - Referred to as looping.
- Form
One or more C statements form the body of the
loop
do statement while (condition)
A Boolean expression used to determine if the
loop is to be repeated.
174.9 Continued
- The do-while loop is executed at least once
because the condition is not evaluated until
AFTER the first iteration of the loop. - If the condition is true, statement is executed
again and then the condition reevaluated. - Looping process is repeated until the condition
is evaluated and found to be false.
18- include ltiostream.gt
- int main()
-
- char Answer
- cout ltlt Circle Area Calculator ltlt endl
- do
- const double PI 3.14159
- double Radius
- cout ltlt Enter the radius
- cin gtgt Radius
- cout ltlt Area is ltlt (PI Radius Radius)ltlt
endl - // Ask if another calculation is desired
- cout ltlt Do another circle (Y/N)?
- cin gtgt Answer
- while (Answer Y)
- cout ltlt Thanks for using the Circle Calculator
ltlt endl - Return (0)
194.11 Looping The while Statement
- Evaluates its condition BEFORE each iteration
- May execute zero or more times.
- Syntax (form) of a while loop body
- while (condition)
- statement
-
20- include ltiostream.hgt
- int main()
-
- int number
- cout ltlt Enter a positive number
- cin gtgt number
- while (number lt 0)
- cout ltlt Number must be positive\n
- cout ltlt Please re-enter
- cin gtgt number
-
214.16 Looping The for Statement
- Used to execute a loop body a fixed number of
times. - Form
- for (initialization, condition, increment)
- statement
Evaluated before each iteration
Performed after each iteration - counter
Performed only once
224.16 Looping The for Statement
- Example
- The following loop display the numbers 1 through
10. -
for (int i 1 i lt 10 i) cout ltlt I ltlt endl
234.17 and -
- Decrement (decrease)
- Total--
- Special form of assignment operator indicates the
value after the operator is to be subtracted from
the value of the variable on the left of the
operator. - Total Total Value
- Total -
24PROGRAMS
- Review 21 and 22
- Exercises
- 14, p. 4-32
- 15, p. 4-32
- 16a, p. 4-33
- 18a, b, c, p. 4-33/34
- 21
- Quiz on Friday (for statements)