Back to DFS's Turing Page


Multiple-Option if Statements

if statements are used to select whether a sequence of statements should be executed or not. In this way, they control the flow of the program. If there is no choice to be made, each statement in a sequence of instructions is simply executed one after another. A real-life example of this would be the following: 1) Get up; 2) Eat breakfast; 3) Go to school.

Let us continue with the breakfast eating problem. For the double-option if statement, we assumed that there either was enough time to prepare some breakfast or there wasn't. In real life, things are usually more complex -- it could depend on how much time you have before you need to leave home. A real-life example of this would be the following:

  1. Get up;
  2. If you have more than 20 minutes then
          (a) eat a hot breakfast
       otherwise
          if you have more than 10 minutes then
                (b) eat a cold breakfast
          otherwise
                (c) grab something to take along;
  3. Go to school.

The line-breaks used here serve to separate off the three possible actions, (a), (b), and (c). The flowchart fragment illustrates the logic involved in Step 2.

The same kind of structure in Turing could be indented as follows, using four levels inside the initial if:

if expressionA then
   statementA
else
   if expressionB then
      statementB
   else
      statementC
   end if
end if

This structure clearly shows that the second if statement is inside the first. This is known as nesting or embedding.

However, it is equally obvious that this method of indentation could quickly result in your Turing code extending past the right edge of your screen, especially if any of the statements were to be replaced by some other more complex statement. Moreover, this kind of indentation obscures the logic of the situation that one of three choices needs to be made. Consequently, a different technique can be employed. Let us rewrite the real-life example in the following way:

  1. Get up;
  2. IF you have more than 20 minutes THEN
       (a) eat a hot breakfast
    OTHERWISE IF you have more than 10 minutes THEN
       (b) eat a cold breakfast
    OTHERWISE
       (c) grab something to take along;
  3. Go to school.

This sort of indentation highlights the fact that there are three possibilities to be selected from. The last choice is known as the default, the one which will be done if none of the tested conditions is found to be true. The corresponding Pascal structure possesses the same advantage. At the same time though, the nesting of one if statement inside another if statement is no longer obvious.

if expressionA then
   statementA
elsif expressionB then
   statementB
else
   statementC
end if

However, the nesting of one if statement inside another if statement is no longer obvious.

if num > 10 then
   put "Your number is too large."
elsif num < 0 then
   put "Your number is too small."
else
   put "Your number is OK."
end if

In the Turing code below, the statement that would be executed if the second boolean expression is true has been broken into two separate statements so that you can see how a compound statement would be handled.

if num > 10 then
   put "Your number is too large."
elsif (num < 0) then
   put "Your number is "..
   put "too small."
else
   put "Your number is OK."
end if

© 1997-2004 DFStermole
Last Modified: 24 Feb 04