ch03 Notes
ch03 Notes
Chapter 3,
Control Statements: Part
1
1 2
3.1 Introduction
Before writing a program to solve a problem, we must
have a thorough understanding of the problem and a
carefully planned approach to solving it.
When writing a program, we must also understand the
types of building blocks that are available and employ
proven program construction techniques.
In this chapter and in Chapter 4, Control Statements:
Part 2, we discuss these issues as we present the theory
and principles of structured programming.
3 4
5 6
1
2023/3/5
7 8
9 10
11 12
2
2023/3/5
13 14
15 16
17 18
3
2023/3/5
19 20
21 22
23 24
4
2023/3/5
25 26
27 28
29 30
5
2023/3/5
31 32
33 34
3.7 while Repetition Statement (cont.) 3.7 while Repetition Statement (cont.)
Consider a program segment designed to find the first The UML activity diagram of Fig. 3.6 illustrates the flow of
power of 3 larger than 100. Suppose the integer control that corresponds to the preceding while statement.
variable product has been initialized to 3. This diagram introduces the UML’s merge symbol, which
When the following while repetition statement finishes joins two flows of activity into one flow of activity.
executing, product contains the result: The UML represents both the merge symbol and the
● int product = 3; decision symbol as diamonds.
The merge symbol joins the transitions from the initial state
while ( product <= 100 ) and from the action state, so they both flow into the
product = 3 * product; decision that determines whether the loop should begin (or
continue) executing.
35 36
6
2023/3/5
37 38
39 40
41 42
7
2023/3/5
43 44
45 46
47 48
8
2023/3/5
49 50
51 52
53 54
9
2023/3/5
55 56
57 58
59 60
10
2023/3/5
61 62
63 64
65 66
11
2023/3/5
67 68
69 70
71 72
12
2023/3/5
73 74
75 76
77 78
13
2023/3/5
79 80
81 82
83 84
14
2023/3/5
85 86
87
15