| |
Control Structures | page 4 of 15 |
There are only three necessary control structures needed to write programs: sequence, selection, and iteration.
Sequence refers to the line-by-line execution as used in your programs so far. The program enters the sequence, does each step, and exits the sequence.
Selection is the control structure that allows choice among different directions. Java provides different levels of selection:
- One-way selection with an
if structure
- Two-way selection with an
if -else structure
- Multiple selection with a
switch structure
Iteration refers to looping. Java provides three loop structures:
while loops
do -while loops
for loops
Of the seven control structures, the if -else and while loop are the most flexible and powerful for problem-solving. The other control structures have their place, but if -else and while are the most common control structures used in Java code.
The diagrams in H.A.8.2, Control Structures in Java, are flowcharts that describe the flow of program control. A statement rectangle in any control structure can be a simple line or even another control structure. A statement can also be a compound statement that consists of multiple statements.
|