Page 64 - Introduction to Programming with Java: A Problem Solving Approach
P. 64

                30 Chapter 2 Algorithms and Design
2.6 Flow of Control and Flowcharts
In the preceding sections, we described various statements—print statements, assignment statements, and input statements—and we focused on the mechanics of how each statement works. Now it’s time to focus on the relationships between statements. More specifically, we’ll focus on flow of control. Flow of control is the order in which program statements are executed. In our discussion of flow of control, we’ll refer to both algorithms and programs. Flow of control concepts apply equally to both.
Flow of control is best explained with the help of flowcharts. Flowcharts are helpful because they are pictures. As such, they help you to “see” an algorithm’s logic. A flowchart uses two basic symbols: (1) rect- angles, which contain commands like print, assign, and input, and (2) diamonds, which contain yes/no ques- tions. At each diamond, the flow of control splits. If the answer is “yes,” flow goes one way. If the answer is “no,” flow goes another way.
The dashed boxes in Figure 2.5 show three standard structures for flow-of-control—a sequen- tial structure, a conditional structure, and a looping structure. The flowchart on the left—the sequential structure—is a picture of the Rectangle algorithm described in Figure 2.2. Sequential structures contain statements that are executed in the sequence/order in which they are written; for example after execut- ing a statement, the computer executes the statement immediately below it. Conditional structures con- tain a yes/no question, and the answer to the question determines whether to execute the subsequent block of statements or skip it. Looping structures also contain a yes/no question, and the answer to the question determines whether to repeat the loop’s block of statements or move on to the statements after the loop.
            Apago PDF Enhancer
     ?
?
    Sequential Conditional Looping
Figure 2.5 Well-structured flow of control
Structured programming is a discipline that requires programs to limit their flow of control to sequen- tial, conditional, or looping structures. A program is considered to be well structured if it can be decom- posed into the patterns in Figure 2.5. You should strive for well-structured programs because they tend to be easier to understand and work with. To give you an idea of what not to do, see Figure 2.6. Its flow of control is bad because there are two points of entry into the loop, and when you’re inside the loop, it’s hard to know what’s happened in the past. When a program is hard to understand, it’s error-prone and hard to fix. Code that implements an algorithm like this is sometimes called spaghetti code be- cause when you draw a flowchart of the code, the flowchart looks like spaghetti. When you see spaghetti, untangle it!
























































































   62   63   64   65   66