Back to DFS's Pascal 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 function words then and otherwise and the three possible actions, (a), (b), and (c). The flowchart fragment illustrates the logic involved in Step 2.

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

IF expressionA
   statementA
ELSE
   IF expressionB THEN
      statementB
   ELSE
      statementC

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 Pascal 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
ELSE IF expressionB THEN
   statementB
ELSE
   statementC

However, the nesting of one if statement inside another if statement is no longer obvious. At the same time, care must be taken, because this indentation can lead to the use of extra semicolons which could cause compile-time errors. Note that there are no semicolons inside the following:

IF num > 10 THEN
   writeln('Your number is too large.')
ELSE IF num < 0 THEN
   writeln('Your number is too small.')
ELSE
   writeln('Your number is OK.')

In the Pascal 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
   writeln('Your number is too large.')
ELSE IF (num < 0)
   BEGIN
      write('Your number is ');
      writeln('too small.')
   END
ELSE
   writeln('Your number is OK.')

© 1997-99 DFStermole
HTMLified: 27 Dec 99