0% found this document useful (0 votes)
10 views

Lesson 3b - Programming With Pascal - Loops

Uploaded by

David Melville
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lesson 3b - Programming With Pascal - Loops

Uploaded by

David Melville
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Loops

For-do LOOP
A for-do loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.

Syntax:
The syntax for the for-do loop in Pascal is as follows:

for < variable-name > := < initial_value > to [down to] < final_value > do S;

Where, the variable-name specifies a variable of ordinal type, called control variable or index variable;
initial_value and final_value values are values that the control variable can take; and S is the body of the
for-do loop that could be a simple statement or a group of statements.

For example:

for i:= 1 to 10 do writeln(i);

Here is the flow of control in a for-do loop:

The initial step is executed first, and only once. This step allows you to declare and initialize any loop
control variables.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of
the loop does not execute and flow of control jumps to the next statement just after the for-do loop.

After the body of the for-do loop executes, the value of the variable in increased or decreased.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body
of loop, then increment step, and then again condition). After the condition becomes false, the for-do
loop terminates.

Example:

program forLoop;
var
a: integer;
begin
for a := 10 to 20 do
begin

https://www.tutorialspoint.com
writeln('value of a: ', a);
end;
end.
When the above code is compiled and executed, it produces following result:

https://www.tutorialspoint.com
Flow Diagram

https://www.tutorialspoint.com

You might also like