Turbo Pascal 2.0 course cover
← Back to Courses
module
12

Making A Choice

Introduction

A program that can only go straight from top to bottom does the same thing every time, whatever it is told. S11 gave your programs a way to be told things. This section gives them a way to act on it: to look at a value and decide.

Comparing

Six signs compare two values:

SignMeans
=equal to
<>not equal to
<less than
>more than
<=less than or equal to
>=more than or equal to

A comparison is either true or false, and Pascal has a type for that: Boolean, whose two values are True and False. You can print one - Writeln(3 < 5) prints TRUE - and keep one in a variable:

var
  Ok: Boolean;
...
Ok := 2 + 2 = 4;

not turns true into false and false into true.

If

if G = 7 then
  Writeln('Right!')
else
  Writeln('Wrong.');

If the comparison is true, the statement after then is done; if it is false, the statement after else is. The else part may be left off, and then nothing is done when it is false.

The condition must be a Boolean. if N then with a number N is Error 20: Boolean expression expected.

There is no semicolon before else. The whole of if ... then ... else ... is one statement, and a semicolon in the middle ends it too early:

if G = 7 then
  Writeln('Right!');  { wrong: this ; }
else

stops with Error 41: Unknown identifier or syntax error, and ESC puts the cursor on the else. It is the commonest mistake in Pascal; everybody makes it; the cursor always shows you where.

More Than One Statement

then and else each take one statement. To do several, wrap them in begin and end, which makes them into one:

if G = 7 then
  Writeln('Right!')
else
begin
  Writeln('Wrong.');
  Writeln('Try again.');
end;

Leave out the begin and end and only the first statement belongs to the else; the second is done every time, whatever the answer.

An if inside an if works as you would hope, with one rule to remember: an else belongs to the nearest if before it that does not already have one. When that is not what you mean, begin and end make it clear.

And, Or

if (G >= 1) and (G <= 10) then
  Writeln('In range.');

and is true when both sides are; or when either is. The brackets round each comparison are needed. Without them Turbo takes the and first and cannot make sense of the rest: if A > 0 and B > 0 then is Error 17: THEN expected.

Comparing Words

Strings can be compared too, and they are compared a character at a time, by the characters' codes. So:

  • 'apple' < 'banana' is true, as you would expect;
  • 'Yes' = 'yes' is false - capitals and small letters are different characters;
  • 'Zebra' < 'apple' is true, because every capital comes before every small letter;
  • '10' < '9' is true, because '1' comes before '9' - strings are not numbers.

Case

When there are many possibilities, a string of ifs becomes hard to read. case chooses one of many by value:

case Mark of
  70..100: Writeln('Grade A');
  50..69: Writeln('Grade B');
  0..49: Writeln('Grade C');
else
  Writeln('Not a mark');
end;

Each line is a label - a value, a list of values separated by commas (6, 8:), or a range (70..100:) - then a colon and a statement. The statement whose label matches is done; if none matches, the one after else is; and if there is no else, nothing is. The case ends with its own end.

case works on integers and characters. It will not work on strings: Error 46: constant and CASE selector type does not match.

Two Promises Kept

S7 said that div by zero gives nonsense silently, and that it was up to you to check. Now you can:

if D <> 0 then
  Writeln(10 div D)
else
  Writeln('Cannot divide by nothing.');

And S11 said a program that must not stop on a bad answer should read a string and check it with Val:

Val(S, N, Code);
if Code <> 0 then
  Writeln('That is not a number.')
else
  Writeln('Twice that is ', N * 2);

The Code

program Guess;
var
  G: Integer;
begin
  Write('Guess my number (1-10): ');
  Readln(G);
  if G = 7 then
    Writeln('Right!')
  else
  begin
    if G < 7 then
      Writeln('Too low.')
    else
      Writeln('Too high.');
    case G of
      6, 8: Writeln('Close.');
      1..5, 9, 10: Writeln('Far.');
    else
      Writeln('That was not 1-10!');
    end;
  end;
end.

Starting from

Turbo freshly started, Y, a new work file called GUESS.

What you should see

It depends on the answer. With 7, Right!. With 3, Too low. and Far.. With 8, Too high. and Close.. With 42, Too high. and That was not 1-10!. Run it several times.

Change One Thing

  • Put a semicolon after Writeln('Right!'). What does Turbo say, and where is the cursor?
  • Take out the begin after the first else, and the end; just before end.. Run it and guess 7. What is printed now, and why?
  • Answer 0. Which of the messages do you get?

Exercises

12.1 Ask for a number and say whether it is even or odd. (mod 2 is 0 for an even number.)

12.2 Ask for two numbers and print the larger.

12.3 Ask for a mark out of 100 and print a grade: 70 and over an A, 50 to 69 a B, below 50 a C - and something sensible for a number that is not a mark at all. Use case.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Error 41 with the cursor on elseA semicolon just before the else. Remove it.
Error 17: THEN expectedand or or between comparisons without brackets round each.
Error 20: Boolean expression expectedThe condition is not a comparison or a Boolean - if N then.
Error 46 on a caseThe value being chosen on is a string. case works on integers and characters.
Part of an else happens every timeSeveral statements after else without begin and end round them.
A string comparison is false when it looks trueCapitals and small letters differ - UpCase the letters first.

Summary

Comparisons give True or False, the values of the type Boolean. if ... then ... else does one statement or the other - never a semicolon before else. begin and end make several statements into one. and, or and not combine conditions, with brackets round each comparison. case chooses among many values and ranges, with else for the rest.

Next

S13, Doing It Again - for, and making the machine repeat itself.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.