
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 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.
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.
| Test | True when |
|---|---|
A=B | A is equal to B |
A<>B | A is not equal to B |
A<B | A is less than B |
A>B | A is more than B |
A<=B | A is less than or equal to B |
A>=B | A 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.
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.
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".
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
>

17, then YES. What does it print, and why is YES not good enough?18. Now change >= in line 20 to > and answer 18 again. What changed?>, type PRINT (5>3);(5<3);(3<=3);(4>=5);(2<>2). Can you say which is which before you look?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.
| Symptom | Cause |
|---|---|
| The right answer is not recognised | Strings must match exactly - YES is not Y, and a small letter is not a capital. |
Something after THEN never happens | The 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 happens | ELSE 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. |
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.
S12, Doing It Again - repeating something a set number of times.