IF and THEN programs

You can make a computer do all sorts of things after an IF and THEN command. For instance, you can tell it to jump to another program line using the command GOTO. You can also make it stop the program.

Using GOTO

In this program you have to guess how many cakes Slimy Sid has eaten. If your guess is wrong, the GOTO command makes the computer jump back to line 50 so you can have another go. The computer will go on doing this until you get the answer right.

NEW
10 CLS
20 LET C=17
30 PRINT "GUESS HOW MANY CAKES"
40 PRINT "SLIMY SID HAS EATEN?"
50 INPUT G
60 IF G<>C THEN GOTO 50
70 PRINT "YOU'VE GUESSED IT!"
RUN

Improving the program

The cake program would be clearer if the computer told you when your guess was wrong. You can do this on most computers, by putting a PRINT command after THEN in line 60.

Try typing in this version of line 60. See what difference it makes to the program.

60 IF G<>C THEN PRINT "WRONG": GOTO 50

IF and THEN with STOP

This password program uses the command STOP after IF and THEN. It tells the computer to stop the program if you type in the wrong password. Most computers display a message when they stop a program. This lets you know what line the program has stopped at.

NEW
10 CLS
20 P$="BAKED BEANS"
30 PRINT "WHAT IS THE PASSWORD?"
40 INPUT A$
50 IF A$<>P$ THEN STOP
60 PRINT "CORRECT"
70 PRINT "WELCOME, FRIEND"
RUN

Can you guess the password correctly? Remember that strings are case-sensitive so you'll need to enter the password using the same uppercase letters as it appears in the program (if you entered it like that). Remember you can always use LIST to list your program.

Maths machine

Try this program. When you run it, you give the computer two numbers. Then you tell it whether to add or multiply them by typing A or M. Lines 80 and 90 use IF and THEN to make the computer do the calculation you asked for; storing the answer in Z.

NEW
10 CLS
20 PRINT "GIVE ME TWO NUMBERS"
30 INPUT X: INPUT Y
40 PRINT "SHALL I ADD"
50 PRINT "OR MULTIPLY?"
60 PRINT "(TYPE A OR M)"
70 INPUT R$
80 IF R$="A" THEN Z=X+Y
90 IF R$="M" THEN Z=X*Y
100 PRINT "THE ANSWER IS ";Z
RUN

Here's a fun challenge! What does the program do if you don't type A or M? Change the program so that if you don't type A or M, it asks you again.