The While Statement

The For Loop

The Repeat - Until Statement

Interactive Example

Examples

Repetitions are used whenever a type of statement is to be executed several times, eg. writing x times 'hello', writing hello till a number has been reached which is not known before but computed in the loop etc. The general name for these 3 Statements is 'loop' because all three execute in a loop.

The While Statement
 
This type of repetition executes until a condition becomes false. This condition is checked via a boolean expression. As soon as this condition becomes true, the repetition block is left and the remaining code is executed.
The format for while -loops is as follows:
An example for a while loop could be to write 'hello' 10 times:
program writehello(input,output);
var
i:integer;
begin
i:=0;
while i<10 do
begin
writeln('hello');
i::=i+1; 
end
end.
Important:
     
  • The boolean expression must be initially true, otherwise the block of statements will not be executed at all, that is you will not enter the loop but skip it right away.
  • It is essential that the statement within the loop eventually changes the value of the condition, otherwise the loop executes an infinite number of times.
  • Execution of while is terminated when the condition becomes true.

The For Loop
 
The for loop is perfectly suitable to execute a statement a number of times, where you specify the number.
There are 2 types of for-loops, the first one is where you can increment the loop-variable, the second one is where you decrease the loop-variable.
The format for the for-loop is as follows:
The examples above, writing 'hello' 10 times could be implemented as follows:
program writehello(input,output);
var
i:integer;
begin
for i:=1 to 10 do
writeln('hello');
end.
The 5.line could also be written as : for i:=10 downto 1 do and nothing would have been changed from the output.
 
 

The Repeat - Until Statement
 
This type of loop is executed until a certain condition becomes true. It has two differences from the while statement:
     
  • The while statement is executed until a condition becomes false, whereas the repeat-until-loop is executed until a condition becomes true. That is in thw while loop the condition is tested before each repetition whereas in the repeat-until loop, the condition is tested after each repetition of the loop.
  • The repeat-until-loop is executed at least once, no matter what the condition variable is in a state, because the check of the variable is located at the end of the statement block, whereas a while-loop need not be executed if the condition variable does not satisfy the boolean expression.
The format for repeat until is:
The example of writing hello 10 times could be implemented as follows:
program writehello(input,output);
var
i:integer;
begin
i:=0;
repeat
writeln('hello');
i:=i+1;
until i=10;
end.
Important:
     
  • There must be at least one statement which has an effect on the terminating condition, other the loop will continue forever as the condition will never be met.
  • The termination condition must eventually be satisfied.
Examples :

repeat example

To find the maximum number of a sequence of numbers ending with -1
 
program maximum(input,output):
const
sentinel = -1;
var
max,num : integer;
begin
max: = -maxint;
writeln('Enter the values');
repeat
read(num);
if num > max then max:=num
until num=sentinel;
writeln(max);
end.

for example

Write a sequence of numbers beginning with 0 and ending with 20 including only the even numbers
 
program even(input,output);
var i:integer;
begin
for i:=0 to 10 do
writeln(i*2);
end.

while example

Same problem as for repeat-until example.
 
.........
begin
max:= -maxint;
writeln('Enter the sequence');
read(num):
while num<>sentinel do
begin
if num > max then max:=num;
read(num);
end;
...........

The following applet is here to illustrate you the basic differences between the 3 loop statements. With this you can, e.g , see the difference of execution begin and stop between while and repeat-until.

In the first box, you can enter a for loop code pice and when you click onto the 'Start For' button, you see the output in the 4. box. The same applies for the while-loop and the 2.box and the repeat-until loop and the 3.box.

There are several restrictions for using this applet. 

  1. It does not handle all possible loop-codes but only specific ones. As this applet is only made to illustrate you the difference between the loop-statements, it only accepts write and writeln's as statement within begin..ends of the statements.
  2. The for-loop should not have any begin..ends in its box. It should only have the loop-statement and a write or writeln-part.
  3. The while loop should be preceded with the initialization of the loop variable. Then comes the while-statement and a begin with a following write or writeln statement, and expression changing the loop-variable and an end.
  4. The same restrictions as while.
Examples for use with the applet:

For the for-loop:
 
for i:=1 to 6 do
writeln('abcde');

For the while-loop:
 
i:=10;
while i>2 do
begin
writeln('abcde');
i := i-3
end;

For the repeat-until loop:
 
i:=1;
repeat
writeln('nilay');
i:=i+2 {no semicoln here, do not forget!}
until i=13;

 
 
Illustrative Example

 
A First Look A First Look at Pascal Basic Input/Output Identifiers & Types
Constants Standard Functions If..Then Statement Case Statement
Repetitions Procedures and Functions Enumerated /Subrange Types Sets
I/O, Text Files Arrays Records Recursion
Pointer How to Debug Previous Exam Questions Advanced
ASCII Code Table Homepage University Homepage Computer Engineering Dept.