Being Fluent with Information Technology
Box 3.1 - Conditional Instructions for a Walk
The sentence shown in (a) is a specification in English of how the batter and runners advance when a batter walks in baseball. The indentations group the true and false alternatives of each conditional phrase. Though the solution in (a) is entirely satisfactory, a programmer would notice the redundancy of this direct solution and might reorganize it as shown in (b). The (b) solution tests for the presence of a runner on base and resolves that case, rather than treating the absence and presence of runners as separate cases. Notice that in program (b) a semicolon is used to separate consecutive statements, and parentheses have been added to assist in understanding the programmer's intent.
Solution (a)
-
If no runner is on 1st base then the batter goes to 1st base
-
otherwise there is a runner on 1st base, so
-
if no runner is on 2nd base then the runner on 1st base
-
goes to 2nd base and the batter goes to 1st base
-
otherwise there are runners on 1st and 2nd, so
-
if no runner is on 3rd base then the runner on 2nd base
-
goes to 3rd, the runner on 1st goes to 2nd base
-
and the batter goes to 1st base
-
otherwise the bases are loaded, so
-
the runner on 3rd goes home, the runner on
-
2nd goes to 3rd, the runner on 1st goes to
-
2nd, and the batter goes to 1st.
Solution (b)
-
If there is a runner on 1st base then,
-
(if there is a runner on 2nd base then,
-
(if there is a runner on 3rd base then
-
the runner on 3rd goes home);
-
the runner on 2nd goes to 3rd);
-
the runner on 1st goes to 2nd);
-
the batter goes to 1st.
Copyright 1999 by the National Academy of Sciences