BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
11

Making A Choice

Introduction

A program that asks a question has to be able to act on the answer - open the door if the answer was yes, not if it was no. That is a choice, and every interesting program is full of them.

IF And THEN

IF A$="Y" THEN PRINT "THE DOOR SLIDES OPEN"

BBC BASIC tests A$="Y". If it is true, it does what comes after THEN. If it is false, it does nothing else on that line and goes on to the next.

That "nothing else on the line" is worth remembering. Anything you put after THEN on the same line - even several things - only happens when the test is true.

ELSE

IF AGE>=18 THEN PRINT "YOU MAY VOTE" ELSE PRINT "NOT YET"

ELSE gives the other choice: what to do when the test is false. Exactly one of the two happens. The whole thing has to fit on one line.

The Tests

TestTrue when
A=BA is equal to B
A<>BA is not equal to B
A<BA is less than B
A>BA is more than B
A<=BA is less than or equal to B
A>=BA is more than or equal to B

Watch the edges. AGE>=18 is true at 18; AGE>18 is not.

They work on strings too. A$="Y" is true only if A$ is exactly Y - not YES, not Y with a space, and not a small y. For text, "equal" means character for character.

AND And OR

Two tests can be joined. AND is true only when both are:

IF A$<>"Y" AND A$<>"N" THEN PRINT "PLEASE TYPE Y OR N"

OR is true when either is, or both.

What True Is

A test is really a small sum with an answer. Try it at the >:

PRINT (5>3);(5<3)

prints -1 and 0. True is -1 and false is 0. IF goes the THEN way for anything that is not 0, so you will sometimes see IF COUNT THEN meaning "if COUNT is not zero".

The Code

10 INPUT "HOW OLD ARE YOU",AGE
20 IF AGE>=18 THEN PRINT "YOU MAY VOTE" ELSE PRINT "NOT YET"
30 INPUT "OPEN THE DOOR (Y/N)",A$
40 IF A$="Y" THEN PRINT "THE DOOR SLIDES OPEN"
50 IF A$<>"Y" AND A$<>"N" THEN PRINT "PLEASE TYPE Y OR N"
60 PRINT "DONE"

Starting from

A fresh boot, or NEW.

What you should see

Answering 41 and Y:

>RUN
HOW OLD ARE YOU? 41
YOU MAY VOTE
OPEN THE DOOR (Y/N)? Y
THE DOOR SLIDES OPEN
DONE
>

Choices made from the answers

Change One Thing

  • Run it again and answer 17, then YES. What does it print, and why is YES not good enough?
  • Answer 18. Now change >= in line 20 to > and answer 18 again. What changed?
  • At the >, type PRINT (5>3);(5<3);(3<=3);(4>=5);(2<>2). Can you say which is which before you look?

Exercises

11.1 Ask for two numbers and print the bigger one. (INPUT can ask for two at once: INPUT "TWO NUMBERS",A,B, answered 3,8.)

11.2 Ask for a password, and print WELCOME if it is EINSTEIN and GO AWAY if not.

11.3 Ask for a whole number and print whether it is EVEN or ODD. (What is left over when an even number is divided by 2?)

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
The right answer is not recognisedStrings must match exactly - YES is not Y, and a small letter is not a capital.
Something after THEN never happensThe test was false, and a false IF skips everything else on its line.
The edge case goes the wrong way> and >= differ exactly at the edge.
An ELSE on a line of its own never happensELSE must be on the same line as its IF. On a line by itself it is not an error - it is simply never carried out.

Summary

IF test THEN this ELSE that, all on one line. Tests compare with = <> < > <= >=, on numbers or on strings, exactly. AND and OR join tests. True is -1 and false is 0.

Next

S12, Doing It Again - repeating something a set number of times.

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.