For Loop and While Loop in Pascal Programming – Lesson 7

Last updated on October 8th, 2023 at 03:14 pm

Grammarly Writing Support

In this article, we seek to explore the for loop and while loop in Pascal programming. Loops make our lives easier. They are great tools in programming when we have large amounts of repetitive processes to run.

In this article, we will explore:

  • Examples of how to set up and use the for and while loops;
  • Writing the for and while loops in both algorithms and Pascal programming; and
  • A trace table of the looping structure.

What are the for and while loops used for?

In Pascal programming, the for and while loops allow us to reduce thousands of lines of code in order to perform repetitive tasks. In programming, a loop repeatedly processes pieces of code until some condition is no longer true.

Example 1 – Loops

Write a program that accepts the names of three students as input, number and print each child’s name.

The problem defining diagram

InputProcessOutput
childName1. Get the childNameCount
2. Set the numberchildName
3. Print the childName and number.

Pseudocode and flowchart of the loop above

Algorithm childAge;

start

  1. count = 1.
  2. while(count<=3)
    • a. Display: What is your child’s name
    • b. Read: childName
  3. Display: count
  4. Display: childName
  5. Add 1 to count

stop

Image of the flow algorithm for the while loop.

Test data – Names

  1. Sue Ann
  2. Misty
  3. Arlene

Trace table of the loop in example 1

Stepcountcount<=3childNameOutput
11
2true
2 aWhat is your child’s name?
2 bSue Ann
31
4Sue Ann
52
2true
2 aWhat is your child’s name?
2 bMisty
32
4Misty
53
2true
2 aWhat is your child’s name?
2 bArlene
33
4Arlene
54
2False

Example of the Pascal program using the while loop

program childrenName;

var
count : integer;

childName : string;

begin
count := 1;

while(count<=3)do

    begin

    writeln('Please enter the name of your child:');

    readln(childName);

    write(count,'.');  
    writeln(childName); 
    count := count + 1;

end;

end.

Loops are often used with control structures and arrays to make our work easier. If you wish to explore control structures, be sure to explore our articles on case statements.

Example 2 – Loop

Write a program that takes in several employees’ names and years of service. The program should check their years of service and print the names of all the employees with more than 5 years of service.

The problem defining diagram of example 2

InputProcessOutput
names1. Read names.names
yearsOfService2. Read the yearsOfService.
3. Find the names of all the persons who have served for more than 5 years.
4. Print names

The Pseudocode and flowchart of the for loop example 2

Algorithm years

string names

integer yearsOfService

integer count

start

  1. for(count = 1 to 3) start
    • a. Display: Please enter the names of the employees
    • b. Read: names
    • c. Display: Please enter the years of service
    • d. Read yearsOfService
    • e. if(yearsOfService>5)
      • i Display: names
  2. stop

stop.

Algorithm showing the for loop of example 2.

Test data

NamesYearsofService
Aalyah Bowen5
Louis Smith6
Jason Lance2

Trace table of the for loop example 2

StepscountNamesYearsOfServiceYearsOfService
>5
Output
11
1 aPlease enter the names of your employees.
1 bAalyah Bowen
1 cPlease enter the years of service.
1 d5
1 efalse
12
1 aPlease enter the names of your employees.
1 bLouis Smith
1 cPlease enter the years of service.
1 d6
1 etrue
1 e iLouis Smith
13
1 aPlease enter the names of your employees.
1 bJason Lance
1 cPlease enter the years of service.
1 d2
1 efalse

Pascal program of the for loop – example 2

program yearsServed;

var
names : string;
yearsOfService : integer;
count : integer;

begin
for count := 1 to 3 do
begin
    writeln('Please enter the names of the employees:');
    readln(names);
    writeln('Please enter the years of service of this employee:');
    readln(yearsOfService);
    if(yearsOfService>3)then
    begin
        writeln(names);
    end;
end;
end.

If you are interested in learning more about the for and while loops, you should explore our article on arrays.

Before you go

We have multiple articles exploring programming in Pascal, which you should explore to become a better Pascal programmer. Be sure to check them out.

We try our best to be as thorough as possible in the information we provide. However, if you have any questions or comments, be sure to leave them in the comment section below, and we will get back to you.