Loops are used to execute a block of code multiple times. Pascal provides three distinct loop types suitable for different scenarios: for, while, and repeat...until.
1. The for Loop (Counted Iteration)
The for loop is used when you know exactly how many times the loop should execute. It iterates a control variable over a specific range of values.
- Control Variable: Must be an ordinal type (usually
IntegerorChar) and must be declared in thevarsection. It is automatically incremented or decremented by 1.
A. Ascending Loop (to)
// Executes when the start value is less than or equal to the end value.
for ControlVariable := StartValue to EndValue do
begin
// Statements to be executed repeatedly
end;
var
i: Integer;
begin
// Loop executes 5 times (i = 1, 2, 3, 4, 5)
for i := 1 to 5 do
Writeln('Iteration number ', i);
end.
B. Descending Loop (downto)
// Executes when the start value is greater than or equal to the end value.
for ControlVariable := StartValue downto EndValue do
begin
// Statements
end;
var
i: Integer;
begin
// Loop executes 3 times (i = 3, 2, 1)
for i := 3 downto 1 do
Writeln('Countdown: ', i);
end.
2. The while Loop (Pre-Test Loop)
The while loop is used when the number of iterations is unknown and the loop should continue as long as a condition remains True. The condition is checked before the code block is executed.
- Pre-Test: If the condition is initially
False, the loop body will never execute.
// Syntax
while BooleanExpression do
begin
// Statements to execute while the expression is True
end;
var
Counter: Integer;
begin
Counter := 5;
while Counter > 0 do // Check condition before each iteration
begin
Writeln('Remaining: ', Counter);
Counter := Counter - 1; // Must change the condition inside the loop!
end; // Loop stops when Counter is 0
end.
Caution: If the condition never becomes
False, the loop will run forever, resulting in an infinite loop.
3. The repeat...until Loop (Post-Test Loop)
The repeat...until loop is similar to while, but the condition is checked after the code block executes.
- Post-Test: The code block is guaranteed to execute at least once, regardless of the initial condition.
- Condition: The loop continues until the condition becomes
True.
// Syntax: No 'begin...end' needed for the block
repeat
// Statements (executed at least once)
Statement1;
Statement2;
until BooleanExpression; // Loop stops when expression is True
var
Password: String;
begin
repeat
Write('Enter Password: ');
Readln(Password);
until Password = 'secret'; // Loop continues until the password is correct
Writeln('Login successful!');
end.
4. Loop Control Statements
These keywords allow you to alter the normal flow of execution within any loop structure (for, while, or repeat...until).
| Keyword | Purpose | Effect |
break | Immediately exits the innermost loop, regardless of the loop condition, and continues execution at the statement following the loop. | Used to stop iteration early (e.g., after finding a search result). |
continue | Immediately skips the rest of the current iteration’s code block and jumps to the next iteration (or re-checks the condition). | Used to skip processing based on a condition (e.g., skip processing negative numbers). |
var
i: Integer;
begin
for i := 1 to 10 do
begin
if i = 6 then
break; // Exit the loop entirely when i reaches 6
if (i mod 2) = 0 then
continue; // Skip the rest of this iteration if i is even
Writeln('Odd number: ', i);
end;
// Execution continues here after the break
Writeln('Loop ended.');
end.
