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)

  1. If no runner is on 1st base then the batter goes to 1st base
  2. otherwise there is a runner on 1st base, so
  3. if no runner is on 2nd base then the runner on 1st base
  4.    goes to 2nd base and the batter goes to 1st base
  5.    otherwise there are runners on 1st and 2nd, so
  6.    if no runner is on 3rd base then the runner on 2nd base
  7.       goes to 3rd, the runner on 1st goes to 2nd base
  8.       and the batter goes to 1st base
  9.       otherwise the bases are loaded, so
  10.       the runner on 3rd goes home, the runner on
  11.       2nd goes to 3rd, the runner on 1st goes to
  12.       2nd, and the batter goes to 1st.

Solution (b)

  1. If there is a runner on 1st base then,
  2.    (if there is a runner on 2nd base then,
  3.       (if there is a runner on 3rd base then
  4.          the runner on 3rd goes home);
  5.       the runner on 2nd goes to 3rd);
  6.    the runner on 1st goes to 2nd);
  7. the batter goes to 1st.



Copyright 1999 by the National Academy of Sciences