4.2 Flow of Execution

Logic Flow

Every algorithm has some type of logic flow. It is very easy to demonstrate logic flow using a flowchart. Let’s revisit the algorithm for driving from Chapter 1 but this time illustrate it using the flowchart in Figure 4-1.

Flowchart directions of
Figure 4-1. Flowchart directions of Figure 1-1

To properly move through the flowchart, start in the box that has the word Start in it, perform the step in that box, and then follow the arrow to the next box. Continue this process until there is nowhere else to go in the chart. If you walked through this flowchart, you would see that each step is performed once before moving on to the next step. This is known as one-directional flow. Algorithms with a one-directional logic flow do not really warrant a flowchart. However, not all algorithms are that simple, and not all flow is in one direction; that is, multiple possible execution flows exist. For example, Figure 4-2 shows a non-one-directional logical flow that models a choice between the two directions presented in Figures 1-1 and 1-2 found in Chapter 1.

Multiple-path logic flowchart
Figure 4-2. Multiple-path logic flowchart

To move through the flowchart in Figure 4-2, start in the uppermost box and follow the arrow to the next box. In this box, there is a condition and two arrows, but you pick one ...

Get Computer Science Programming Basics in Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.