PASCAL PROGRAMMING


LOOP STATEMENT / STRUCTURE

Loop in Pascal performs repetitive tasks.

For loop

While loop

Repeat until loop

 

Program exercises implementing for-loop in Pascal programming

For loop executes your program a specific number of times.

Syntax:

for variableName := startValue to endValue do

begin

//statements

end;

 

 

Lab1

//display numbers from 1 to 5

program lab1;

var

xcount : integer;

begin

  for xcount := 1 to 5 do

  begin

  writeln (xcount);

  end;

end.

 

Lab2

//display DMACTUTOR five times

program lab2;

var

xcount : integer;

begin

  for xcount := 1 to 5 do

  begin

  writeln ('DMACTUTOR');

  end;

end.

 

Lab3

//accept and display 5 different names from students

program lab3;

var

xcount : integer;

fullname : string;

begin

  for xcount := 1 to 5 do

  begin

  writeln ('Insert your name ');

  readln(fullname);

  writeln ('your name is:: ', fullname);

  end;

end.

OUPTUT SCREENSHOT:



TIP: In the above program, as we enter each name, the for loop prints it one by one. However, if we want the program to store all the names first before printing all the names at the same thing, we will need to use ARRAY concept. We will implement this in our next tutorial.

 

Program exercises implementing while-loop in Pascal programming

Syntax:

Initialization;

while condition do

begin

increment;

//statements

end;

 

 

Lab4

//display numbers from 1 TO 5

program lab4;

var

xcount : integer;

begin

 writeln('before loop begins');

     xcount := 0 ;                  

     while (xcount <= 4) do 

     begin

       xcount += 1;              //you can also use  xcount := xcount + 1;

       writeln('iteration number ', xcount);

     end;

     writeln('after loop ends');

end.

 


Comments

Popular posts from this blog