Tuesday, 28 August 2012

Pascal Programming Lesson #5 - Decisions


if then else

The if statement allows a program to make a decision based on a condition. The following example asks the user to enter a number and tells you if the number is greater than 5:

program Decisions;

var
   i: Integer;

begin
   Writeln('Enter a number');
   Readln(i);
   if i > 5 then
      Writeln('Greater than 5');
end.

Here is a table of the operators than can be used in conditions:

>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
=Equal to
<>Not equal to

The above example only tells you if the number is greater than 5. If you want it to tell you that it is not greater than 5 then we use else. When you use else you must not put a semi-colon on the end of the command before it.

program Decisions;

var
   i: Integer;

begin
   Writeln('Enter a number');
   Readln(i);
   if i > 5 then
      Writeln('Greater than 5')
   else
      Writeln('Not greater than 5');
end.

If the condition is True then the then part is chosen but if it is False then the else part is chosen. This is because the conditions such as i > 5 is a Boolean equation. You can even assign the result of a Boolean equation to a Boolean variable.

program Decisions;

var
   i: Integer;
   b: Boolean;

begin
   Writeln('Enter a number');
   Readln(i);
   b := i > 5;
end.

If you want to use more than 1 condition then you must put each condition in brackets. To join the conditions you can use either AND or OR. If you use AND then both conditions must be true but if you use OR then only 1 or both of the conditions must be true.

program Decisions;

var
   i: Integer;

begin
   Writeln('Enter a number');
   Readln(i);
   if (i > 1) and (i < 100) then
      Writeln('The number is between 1 and 100');
end.

If you want to put 2 or more commands for an if statement for both the then and the else parts you must usebegin and end; to group them together. You will see that this end has a semi-colon after it instead of a full stop.

program Decisions;

var
   i: Integer;

begin
   Writeln('Enter a number');
   Readln(i);
   if i > 0 then
      begin
         Writeln('You entered ',i);
         Writeln('It is a positive number');
      end;
end.

You can also use if statements inside other if statements.

program Decisions;

var
   i: Integer;

begin
   Writeln('Enter a number');
   Readln(i);
   if i > 0 then
      Writeln('Positive')
   else
      if i < 0 then
         Writeln('Negative')
      else
         Writeln('Zero');
end.

Case

The case command is like an if statement but you can have many conditions with actions for each one.

program Decisions;

uses
   crt;

var
   Choice: Char;

begin
   Writeln('Which on of these do you like?');
   Writeln('a - Apple:');
   Writeln('b - Banana:');
   Writeln('c - Carrot:');
   Choice := ReadKey;
   case Choice of
      'a': Writeln('You like apples');
      'b': Writeln('You like bananas');
      'c': Writeln('You like carrots');
   else
      Writeln('You made an invalid choice');
   end;
end.

0 comments:

Post a Comment