NASutils Knowledge BaseUnofficialPolski

Pascal · lesson 10 of 30 · Decisions and loops · bonusRepeat with loops

for counts up or down, while tests first, repeat tests last.

Programming on the NAS › Pascal in 30 lessons

10-loops.pas Download 10-loops.pas

program Loops;
var
  I, Left, Tries: Integer;
begin
  for I := 1 to 5 do
    Write(I, ' ');
  WriteLn;

  for I := 3 downto 1 do
    Write(I, ' ');
  WriteLn('start');

  Left := 100;
  while Left > 20 do
  begin
    Left := Left div 2;
    WriteLn('half is ', Left);
  end;

  Tries := 0;
  repeat
    Inc(Tries);
    WriteLn('try ', Tries);
  until Tries = 3;
end.

Terminal

$ ./10-loops
1 2 3 4 5 
3 2 1 start
half is 50
half is 25
half is 12
try 1
try 2
try 3

Exercise

Print all even numbers from 2 to 20.