How to implement loops in Pascal
What are loops?
Loops are programming structures that are used to repeat a block of program statements until a given condition is met.
Loops in Pascal
There are three types of loops in Pascal:
- Fixed repetition loop: Executes the block a fixed number of times.
- Pretest loop: Checks if the loop condition is met, then executes the block.
- Posttest loop: Executes the block, then checks if the loop condition is met.
Fixed repetition loop
The fixed repetition loop is initialized as follows:
for _loopVariable_ := _start_ to _end_ do
begin
_block_
end;
_loopVariable_: The variable name of the loop variable that will iterate from_startto_end_._start_: The starting value of the loop._end_: The ending value of the loop._block_: The program statements that will be executed repeatedly.
Flowchart
Example
Let’s look at the code snippet below, which demonstrates the implementation of a fixed repetition loop in Pascal.
program fixedRepetetion;vari: integer;beginwriteln('Loop in forward direction');for i := 0 to 3 dobeginwriteln(i, ': Hello world!! This loop is executing in forward direction');end;for i := 3 to 2 dobeginwriteln(i, ': Hello world!! This loop is executing in forward direction');end;writeln(sLineBreak + 'Loop in backward direction');for i := 3 downto 0 dobeginwriteln(i, ': Hello world!! This loop is executing in backward direction');end;end.
Explanation
- Lines 7-10: Loop iterates in the forward direction from 0 to 3.
- Lines 12-15: Loop iterates 0 number of times. This is because the starting value of the loop is greater than the ending value of the loop, i.e., 3>2.
- Lines 18-21: Loop iterates in the backward direction from 3 to 0. This is because of the
downkeyword used in theforstatement.
Pretest loop
The pretest loop is initialized as follows:
while _loopCondition_ do
begin
_block_
end;
_loopCondition_: The condition that is checked every time before executing theblock. Theblockis executed only if this condition is true._block_: The program statements that will be executed repeatedly.
Flowchart
Example
Consider the code snippet below, which demonstrates the implementation of a pretest loop in Pascal.
program pretest;vari: integer;begini := 0;while i < 3 dobeginwriteln (i, ': Hello World!!');i := i + 1;end;end.
Explanation
- A variable
iis initialized with 0 in line 7. The while loop in line 8 checks ifi<3in line 8; this is the loop condition. The loop will iterate until this condition becomes false. Asiis incremented in line 11 each time the block executes, the while loop is executed 3 times. The third time the block is executed,ibecomes 4. Thus, the condition ofwhilewill not be met and the loop will break.
Posttest loop
The posttest loop is initialized as follows:
repeat
begin
_block_
end;
until _loopCondition_ ;
_loopCondition_: The condition that is checked every time after executing theblock. The loop breaks if the condition is met._block_: The program statements that will be executed repeatedly.
Flowchart
program posttest;vari: integer;begini := 0;repeatbeginwriteln (i, ': Hello World!!');i := i + 1;enduntil i>3;end.
Explanation
A variable i is initialized with 0 in line 6. The loop block, which is line 9-10, is executed before checking the loop condition in line 12. The loop block will execute repeatedly until the condition in line 12 is met. As i is incremented in line 10, each time the block executes, the repeat until loop is executed 4 times. The fourth time the block is executed, i becomes 4. Thus, the condition of until will be met and the loop will break.
Free Resources