Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

How to implement loops in Pascal

Kainat Asif

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 _start to _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

Fixed loop - Flowchart

Example

Let’s look at the code snippet below, which demonstrates the implementation of a fixed repetition loop in Pascal.

program fixedRepetetion;
var
   i: integer;

begin
   writeln('Loop in forward direction');
   for i := 0  to 3 do
   begin
      writeln(i, ': Hello world!! This loop is executing in forward direction');
   end;
   
   for i := 3  to 2 do
   begin
      writeln(i, ': Hello world!! This loop is executing in forward direction');
   end;
   
   writeln(sLineBreak + 'Loop in backward direction');
   for i := 3  downto 0 do
   begin
      writeln(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 down keyword used in the for statement.

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 the block. The block is executed only if this condition is true.
  • _block_: The program statements that will be executed repeatedly.

Flowchart

Pretest loop - Flowchart

Example

Consider the code snippet below, which demonstrates the implementation of a pretest loop in Pascal.

program pretest;
var
   i: integer;

begin
   i := 0;
   while i < 3 do
   begin
      writeln (i, ': Hello World!!');
      i := i + 1;
   end;
end.

Explanation

  • A variable i is initialized with 0 in line 7. The while loop in line 8 checks if i<3 in line 8; this is the loop condition. The loop will iterate until this condition becomes false. As i is incremented in line 11 each time the block executes, the while loop is executed 3 times. The third time the block is executed, i becomes 4. Thus, the condition of while will 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 the block. The loop breaks if the condition is met.
  • _block_: The program statements that will be executed repeatedly.

Flowchart

Posttest loop - Flowchart

Example

Consider the code snippet below, which demonstrates the implementation of a posttest loop in Pascal.

program posttest;
var
   i: integer;

begin
   i := 0;
   repeat
   begin
      writeln (i, ': Hello World!!');
      i := i + 1;
   end
   until 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.

RELATED TAGS

pascal

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring