previous page main index next page
 

Decisions, decisions...

Our programs so far have broken down into a list of steps, one after the other. Sometimes, however, we need to make decisions and choose between two or more options.

Pascal uses the keywords IF, THEN and ELSE when making choices.

Example 12

This is a simple program that reads in a person's age and prints out a message.

  • type in the program below
  • save it as voting.pas
     
PROGRAM voting;       {to decide if you can vote or not}

VAR
  age  : INTEGER;
  name : STRING[20];

BEGIN
  WRITE('enter your name ');
  READLN(name);
  WRITE('enter your age ');
  READLN(age);
  WRITELN('hello ', name);
  IF age < 18
  THEN
    WRITELN('you are too young to vote')
  ELSE
    WRITELN('vote for the purple party!')
END.

  • run this program and check it using different input data
     

Note that only one of the two messages is displayed, depending on the person's age.
 

previous page main index next page
 
© 2001 by Mike Hardy