ch04_notes
ch04_notes
Chapter 4,
Control Statements:
Part 2
C++ How to Program,
Late Objects Version, 7/e
1 2
4.1 Introduction
for, do…while and switch statements.
counter-controlled repetition.
Introduce the break and continue program control
statements.
Logical operators for more powerful conditional
expressions.
Examine the common error of confusing the equality
(==) and assignment (=) operators, and how to avoid
it.
Summarize C++’s control statements.
3 4
5 6
1
2023/3/13
7 8
9 10
4.3 for Repetition Statement (cont.) 4.3 for Repetition Statement (cont.)
If the initialization expression declares the control The initialization and increment expressions can be
variable, the control variable can be used only in the comma-separated lists of expressions.
body of the for statement—the control variable will be The commas, as used in these expressions, are comma
unknown outside the for statement. operators, which guarantee that lists of expressions
This restricted use of the control variable name is evaluate from left to right.
known as the variable’s scope. ◦ The lowest precedence of all C++ operators.
The scope of a variable specifies where it can be used The value and type of a comma-separated list of
in a program. expressions is the value and type of the rightmost
expression.
11 12
2
2023/3/13
4.3 for Repetition Statement (cont.) 4.3 for Repetition Statement (cont.)
The three expressions in the for statement header are The increment expression in the for statement acts as a
optional (but the two semicolon separators are standalone statement at the end of the body of the for.
required). The expressions
If the loopContinuationCondition is omitted, C++ ● counter = counter + 1
assumes that the condition is true, thus creating an counter += 1
infinite loop. ++counter
One might omit the initialization expression if the counter++
control variable is initialized earlier in the program. are all equivalent in the incrementing portion of the for
One might omit the increment expression if the statement’s header (when no other code appears there).
increment is calculated by statements in the body of the
for or if no increment is needed.
13 14
4.3 for Repetition Statement (cont.) 4.3 for Repetition Statement (cont.)
The initialization, loop-continuation condition and The for repetition statement’s UML activity diagram is
increment expressions of a for statement can contain similar to that of the while statement (Fig. 4.6).
arithmetic expressions. Figure 4.4 shows the activity diagram of the for
The “increment” of a for statement can be negative, in statement in Fig. 4.2.
which case the loop actually counts downward. The diagram makes it clear that initialization occurs
If the loop-continuation condition is initially false, the once before the loop-continuation test is evaluated the
body of the for statement is not performed. first time, and that incrementing occurs each time
through the loop after the body statement executes.
15 16
17 18
3
2023/3/13
19 20
21 22
23 24
4
2023/3/13
4.4 Examples Using the for Statement 4.4 Examples Using the for Statement
(cont.) (cont.)
Parameterized stream manipulators setprecision- and Stream manipulator fixed indicates that floating-point
setw and the nonparameterized stream manipulator fixed.
The stream manipulator setw(4) specifies that the next values should be output as fixed-point values with
value output should appear in a field width of 4—i.e., cout decimal points.
prints the value with at least 4 character positions. Stream manipulator setprecision specifies the number
◦ If less than 4 character positions wide, the value is right justified in
the field by default. of digits to the right of the decimal point.
◦ If more than 4 character positions wide, the field width is extended
to accom-modate the entire value. Stream manipulators fixed and setprecision remain in
To indicate that values should be output left justified, effect until they’re changed—such settings are called
simply output nonparameterized stream manipulator left . sticky settings.
Right justification can be restored by outputting
The field width specified with setw applies only to the
nonparameterized stream manipulator right.
next value output.
25 26
27 28
29 30
5
2023/3/13
31 32
33 34
35 36
6
2023/3/13
37 38
39 40
41 42
7
2023/3/13
43 44
45 46
47 48
8
2023/3/13
49 50
51 52
53 54
9
2023/3/13
55 56
57 58
59 60
10
2023/3/13
61 62
4.9 Confusing the Equality (==) and 4.9 Confusing the Equality (==) and
Assignment (=) Operators Assignment (=) Operators (cont.)
Accidentally swapping the operators == (equality) and = Variable names are said to be lvalues (for “left values”)
(assignment).
Damaging because they ordinarily do not cause syntax errors. because they can be used on the left side of an
Rather, statements with these errors tend to compile correctly and assignment operator.
the programs run to completion, often generating incorrect
results through runtime logic errors. Constants are said to be rvalues (for “right values”)
[Note: Some compilers issue a warning when = is used in a because they can be used on only the right side of an
context where == typically is expected.] assignment operator.
Two aspects of C++ contribute to these problems.
◦ One is that any expression that produces a value can be used in the Lvalues can also be used as rvalues, but not vice versa.
decision portion of any control statement.
◦ The second is that assignments produce a value—namely, the value
assigned to the variable on the left side of the assignment operator.
Any nonzero value is interpreted as true
63 64
65 66
11
2023/3/13
67 68
69 70
71 72
12
2023/3/13
73
13