typestar

while and repeat in Delphi

One three-minute kiln countdown written twice, once in each loop form.

program WhileRepeat;
uses SysUtils;
var
  Minutes: Integer;
begin
  // while tests first, so a start of zero runs the body zero times.
  Minutes := 3;
  while Minutes > 0 do
  begin
    WriteLn(Format('kiln timer at %d', [Minutes]));
    Dec(Minutes);
  end;
  // repeat tests last, so the body always runs at least once.
  Minutes := 3;
  repeat
    WriteLn(Format('glaze timer at %d', [Minutes]));
    Dec(Minutes);
  until Minutes = 0;
  WriteLn('both timers reached zero');
end.

How it works

  1. while Minutes > 0 do tests first, so a start of zero would print nothing at all.
  2. repeat tests last at until Minutes = 0;, so its body always runs at least once.
  3. Both bodies call Dec(Minutes) so the counter actually reaches the exit condition.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
444
Tokens
81
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 61 seconds.

Type this snippet

Step 3 of 3 in Control flow, step 9 of 27 in Language basics.

← Previous Next →