NASutils Knowledge BaseUnofficialPolski

Pascal · lesson 7 of 30 · Decisions and loops · bonusDecide with if

if, then and else choose a block; begin and end group several statements. There is no semicolon before else.

Programming on the NAS › Pascal in 30 lessons

07-if.pas Download 07-if.pas

program Temperature;
var
  Temp: Integer;
begin
  Write('Disk temperature in C: ');
  ReadLn(Temp);
  if (Temp < 0) or (Temp > 100) then
    WriteLn('Type a number from 0 to 100')
  else if Temp >= 55 then
    WriteLn('Too hot: check the fans')
  else if Temp >= 45 then
  begin
    WriteLn('Warm');
    WriteLn('Look again in an hour');
  end
  else
    WriteLn('Fine');
end.

Terminal

$ ./07-if
Disk temperature in C: 38
Fine
$ ./07-if
Disk temperature in C: 47
Warm
Look again in an hour
$ ./07-if
Disk temperature in C: 900
Type a number from 0 to 100

Exercise

Ask for the free space in percent and answer fine, low or full.