PASCAL Programming: § 4: Selection and Iteration Structures

Instructor: M.S. Schmalz


A key feature of computer programs is decision making, where the program selects a result from among a list of alternatives. The PASCAL statements that support such decision making are collectively called selection structures and include the IF..THEN..ELSE and CASE statements. We will later see that the CASE statement is not in vogue (i.e., isn't used much) because it is harder to read clearly than an IF statement.

Computer program design is also facilitated by the use of program structures called loops that cause the block of statements within the loop to repeat or iterate. The latter term gives rise to the concept of iteration structures, which in PASCAL include the FOR, WHILE, and REPEAT statements.

This section is organized as follows:

In Section 4.1, we discuss the basic justification and concepts associated with iteration and selection structures, with examples of modularity in the block-IF statement. We then discuss usage of the IF and CASE statements (Section 4.2). An overview of iteration structures is given in Section 4.3, with usage of the WHILE, FOR, and REPEAT statements discussed in Section 4.4. The final topic of this section is the use of variable loop limits, which allows you to "customize" a loop with limit values that are passed into a procedure through its argument list.

4.1. Overview of Selection Structures.

We begin with several observations about decision making in computer programs.

In the 1970s, programming language designers were looking for simpler ways to write statements that were previously involved expressions. Since the programmers made more errors on complex statements, it seemed reasonable to attempt to distill the BLOCK-IF into a more concise representation. In PASCAL, this representation is called the CASE statement. Instead of relational operators, the CASE statement simply specifies possible values for a variable, together with statements to be executed if that variable has a given value. For example, consider the following CASE statement (written in pseudocode) for a academic grade evaluation:

       CASE OF grade:
          'A':  WRITELN('Excellent work!');
          'B':  WRITELN('You did well...');
          'C':  WRITELN('Average performance.');
          'D':  WRITELN('Needs some improvement...');
          'E':  WRITELN('Trouble ahead!');
       END-CASE
Certainly this simple instance of the CASE statement is easier to read than the corresponding BLOCK-IF statement:
       IF grade == 'A' THEN
          WRITELN('Excellent work!')
       ELSEIF grade == 'B' THEN
          WRITELN('You did well...');
       ELSEIF grade == 'C' THEN
          WRITELN('Average performance.');
       ELSEIF grade == 'D' THEN
          WRITELN('Needs some improvement...');
       ELSEIF grade == 'E' THEN
          WRITELN('Trouble ahead!');
       ENDIF
The problem with CASE statements occurs not when the statement blocks are small, or when there are a few alternatives. Rather, the CASE statement begins to defeat its design goal of conciseness and readability when the blocks of statements become large or when there are many alternatives. In such expressions, a given CASE statement may span several pages or more. When you are trying to debug such statements, it becomes very difficult to remember where you are in the statement, and one tends to feel lost. As a result, we prefer to use only IF or BLOCK-IF statements for decision structures.

4.2. PASCAL Selection Structures.

The PASCAL language provides IF or BLOCK-IF constructs, where the latter is a variation of the IF statement, as discussed in Section 3.1. We define these statements as follows:

IF..THEN..ELSE statement:

IF..THEN..ELSEIF..THEN..ELSE (Block-IF) statement:

PASCAL also supports nested decision structures, in which an IF statement contains other IF statements in its list of executable statements. This allows the programmer to specify decisions based on concepts or criteria that are hierarchically structured.

4.3. Overview of Iteration Structures.

We begin with several observations about iteration in computer programs.

We next examine the PASCAL syntax of the FOR, WHILE, and REPEAT loops.

4.4. PASCAL Iteration Structures.

PASCAL supports the following three iteration structures:

FOR loop:

WHILE..DO loop:

REPEAT..UNTIL loop:

In the example of the FOR loop, we saw how a variable could be used to specify a loop index. We next elaborate this technique, and show how it can be used to encapsulate a loop in a procedural construct.

4.5. Variable Loop Limits.

It is not difficult to use PASCAL constructs to write flexible loops. Here follows a simple example of a loop encapsulated in a procedure:
   PROGRAM TestLoop;              {Program specification}
     VAR x,y: integer;            {Declare variables x,y as integer}

     PROCEDURE Loop(x,y:integer); {Function specification: inputs}
       VAR i: integer;            {Declare loop index}
       BEGIN                      {Function begins here}
         FOR i := x TO y DO       {Loop specification}
	   WRITELN('i = ', i);    {Loop contents or body}
       END;                       {Function ends here}

     BEGIN                        {Program begins here}
       x := 4;                    {Assign starting loop index value}
       y := 7;                    {Assign ending loop index value}
       Loop(x,y);                 {Call to procedure "Loop"}
     END.                         {Program ends here}
In the preceding PASCAL code, note that the VAR statement specifies a variable of integer datatype. In this case, the variables x and y are specified as integers. Additionally, the WRITELN statement outputs the value of i to the screen, as follows:
     i = 4
     i = 5
     i = 6
     i = 7
per the preceding discussion.

Variable loop limits are especially useful when performing tasks in image and signal processing or database manipulations, where the data structure size is parameterized, i.e., can be changed by changing a value encoded in a variable. For example, one often processes images of varying size, cuts or pastes parts of images, etc. By having variable loop indices, you do not need to recode each loop explicity with different limits for different sized images. Once you know how large the image is, you can merely pass the loop limits through the procedure call's argument list and constrain processing to any size image or neighborhood of an image that is within the prespecified array limits.

Good software engineering practice dictates that all loop limits be specified in terms of variables if the loop is to be called from within a procedure, and the loop limits are at any time expected to be flexible. Rather than having to recode a program with variable loop limits (which can be a difficult task), variable limits that are already implemented make the task of resizing a loop much easier. As noted previously, this facilitates modularity and portability of PASCAL code, and makes debugging much easier.


This concludes our overview of selection and iteration structures in PASCAL.
We next discuss PASCAL files, file I/O, and arrays.

Copyright © 1997 by Mark S. Schmalz
All rights reserved except printing by UF students registered for this class.