100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Loops in Pascal

Master Pascal's three loop constructs — for, while, and repeat-until — and know when to use each.

Control Flow & ProceduresBeginner9 min readJul 10, 2026
Analogies

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.

pascal
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.

pascal
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

Was this page helpful?

Topics covered

#Programming#PascalStudyNotes#LoopsInPascal#Loops#Pascal#Repeating#Work#StudyNotes#SkillVeris#ExamPrep