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
while Minutes > 0 dotests first, so a start of zero would print nothing at all.repeattests last atuntil Minutes = 0;, so its body always runs at least once.- Both bodies call
Dec(Minutes)so the counter actually reaches the exit condition.
Keywords and builtins used here
DecFormatIntegerWriteLnbegindoendprogramrepeatuntilusesvarwhile
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.
Step 3 of 3 in Control flow, step 9 of 27 in Language basics.