Repeating Work with Loops
Loops let a Pascal program repeat a block of statements without duplicating code, and the language offers three distinct forms — for, while, and repeat-until — each suited to a different kind of repetition. Choosing the right one depends on whether you know the iteration count in advance, whether the test happens before or after the body runs, and whether the loop must execute at least once regardless of the condition.
Cricket analogy: A bowler running in to deliver exactly six balls in an over before switching ends is a for loop — the iteration count, six, is fixed and known in advance.
The for Loop
The for loop has the form for <variable> := <start> to <end> do <statement> (or downto to count backward), and it is used precisely when the number of iterations is known before the loop begins. The loop control variable is implicitly incremented (or decremented) by the runtime after each iteration, and standard Pascal disallows modifying it inside the loop body — doing so produces undefined behavior in strict implementations.
Cricket analogy: Counting down the final over ball by ball from six to one as the required run rate climbs is exactly a for ball := 6 downto 1 do loop, with the countdown direction baked into the loop's structure.
program SumOfSquares;
var
i, total: Integer;
begin
total := 0;
for i := 1 to 10 do
total := total + (i * i);
WriteLn('Sum of squares 1..10 = ', total);
end.The while Loop
The while loop tests its condition before each iteration: while <boolean expression> do <statement>. Because the test happens up front, if the condition is already false the very first time it is checked, the loop body never executes at all — this makes while the right choice when the number of repetitions isn't known ahead of time and could even be zero.
Cricket analogy: A team chasing a target keeps sending batsmen in while wickets remain and the target isn't reached, and if the target is already reached before the next man walks in, that batsman never bats at all — the pre-test skips the body entirely.
Because while checks its condition before the first iteration, it can execute zero times — this is what distinguishes it from repeat-until, which always executes its body at least once.
The repeat-until Loop
The repeat-until loop has the inverse test placement: repeat <statements> until <boolean expression>;. The body executes first, and the condition is checked afterward — so the loop always runs at least once, and it continues repeating until the condition becomes true (the opposite polarity from while, which continues while the condition is true).
Cricket analogy: A bowler is required to complete the over by repeating deliveries until six legal balls are bowled, so even a no-ball on the very first delivery guarantees at least one ball was bowled before the over-completion check runs.
program GuessNumber;
var
secret, guess: Integer;
begin
secret := 42;
repeat
Write('Guess the number: ');
ReadLn(guess);
if guess <> secret then
WriteLn('Try again.');
until guess = secret;
WriteLn('Correct!');
end.A while or repeat-until loop whose condition never becomes false (or true, for repeat-until) runs forever — always verify that something inside the loop body actually changes a variable used in the condition.
- for loops are used when the number of iterations is known in advance
- while loops test the condition before the body runs, so they can execute zero times
- repeat-until loops test after the body runs, so they always execute at least once
- repeat-until continues until its condition becomes TRUE — the opposite polarity of while
- Modifying a for loop's control variable inside the loop body is unsafe in standard Pascal
- downto counts backward in a for loop, just as to counts forward
- Always ensure the loop's exit condition can eventually be satisfied to avoid an infinite loop
Practice what you learned
1. Which loop construct is guaranteed to execute its body at least once?
2. What happens if a while loop's condition is false the very first time it is checked?
3. In `for i := 10 downto 1 do`, what direction does i move?
4. repeat-until stops looping when its condition becomes...
5. Why is it unsafe to modify a for loop's control variable inside the loop body in standard Pascal?
Was this page helpful?
You May Also Like
Conditionals in Pascal
Learn how Pascal's if-then-else and case statements let programs branch based on boolean conditions.
Recursion in Pascal
Learn how Pascal functions and procedures can call themselves, and how to design correct base cases and recursive cases.
Procedures and Functions
Learn how Pascal separates reusable code into procedures (no return value) and functions (return a value), and how to declare and call each.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics