To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97
Chapter 4 Loops
4-1 Looping
You learned in the previous chapter how to build a repetition loop using the following tools:
- A branching statement such as GOTO to transfer the control of the program to the starting point repeatedly
- A condition to terminate the loop as desired
The condition may be used to test the input value and to terminate the loop when a specific value is received. You may also wish to repeat the process in the loop a specific number of times, in which case you need a counter. The condition in this case is used to test the counter with each round of the loop. This type of loop is called a counted loop. In the following program these elementary tools are used to display the message Sorry, say again.. five times.
The algorithm used in the program is as follows:
- 1. Initialize the counter to zero
- 2. Increment the counter by 1
- 3. Test the counter to see if it is less than or equal to 5
- 4. Display the statement
- 5. Go to step 2
{ ------------------------------ figure 4-1 ------------------------------ }
PROGRAM GoToLoop(OUTPUT);
LABEL
1000; { label declaration }
VAR
Kounter :INTEGER;
BEGIN
Kounter := 0;
1000:
Kounter := Kounter + 1;
IF Kounter <= 5 THEN
BEGIN
WRITELN('Sorry, say again..');
GOTO 1000 { restart }
END;
WRITELN;
WRITELN('Press ENTER to continue..');
READLN
END.
In this program the counter is initialized to the value zero before entering the loop, which begins at the label 1000. Inside the loop, the counter is incremented, then tested to see if its value is less than or equal to 5. If so, the WRITELN statement is executed and the loop is repeated using the GOTO statement. If the condition fails (i.e., the counter exceeds 5) the program ends. The output of this program looks like this:
Sorry, say again..
Sorry, say again..
Sorry, say again..
Sorry, say again..
Sorry, say again..
Press ENTER to continue..
Pascal provides you with ready-made control structures for looping, so you can avoid such messy code. A control structure contains both the branching statement and the condition in one construct.
In this chapter you are introduced to the following constructs:
- The FOR loop
- The WHILE loop
- The REPEAT loop
Each of the three loops has different features that suit different applications.
4-2 The FOR Loop
The FOR loop construct is a counted loop used to repeat a statement or a block of statements a specified number of times. It includes the initialization of the counter, the condition, and the increment.
Look at this example:
{ ------------------------------ figure 4-2 ------------------------------ }
PROGRAM ForLoop(OUTPUT);
VAR
Kounter :INTEGER;
BEGIN
FOR Kounter := 1 TO 5 DO
WRITELN('Sorry, say again..');
WRITELN;
WRITELN('Press ENTER to continue..');
READLN
END.
This program gives the same results as the previous program does, but is simpler and better organized. The FOR loop does the same work done in the previous program. It assigns the control variable (Kounter) the initial value 1, then executes the statement, increments the control variable by one, and repeats the process until the value of the Kounter reaches the final value 5.
The general form of the FOR construct is as follows:
FOR control-variable := expression-1 TO expression-2 DO
statement;
where:
control-variable
| is the loop counter,
|
expression-1
| is the initial value, and
|
expression-2
| is the final value.
|
The control-variable, expression-1, and expression-2 can be of any type except REAL. All three must be of the same type.
TIP: Remember that the FOR construct is one statement ending with a semicolon. If by mistake you add another semicolon, as in the following loop:
FOR Kounter := 1 TO 1000 DO;
WRITELN('Sorry, say again..');
do not be surprised if the loop is executed only once, regardless of the final value of the counter. The semicolon after the DO keyword ends the loop at this point.
The value of the control variable may not be modified inside the loop. Look at this assignment statement inside the loop:
FOR K := 1 TO 10 DO
K := 2
...
Even if the compiler accepts this statement, it will repeal the effect of the loop counter as it sets it to the value 2 all the time. The same rule applies for the initial value and the final value of the control variable.
As usual, you can include as many statements as you want inside the loop by using the BEGIN-END blocks.
|