Lesson 5

IF, THEN, AND, OR, GOTO

All the codewords in this lesson are for allowing you to set one or more “conditions”, and the computer will decide what to do depending on what conditions you set.

Examples of conditions we come across every day are:

IF it is raining THEN wear a raincoat

IF you are hungry THEN eat something

IF you are tired THEN go to bed

IF and THEN Sometimes in BASIC there are two codewords which must be used together. This happens with the IF and THEN codewords. When you use the IF codeword you are telling the computer to test “something” to find out if it is true. If that “something” is true then the computer carries out one task, but if that “something” is not true then the computer will carry out a different task or it might do nothing.

AND The AND codeword tells the computer to test if two or more different conditions are true. So AND can be used with IF and THEN.

OR The OR codeword tells the computer to test if either of two conditions is true. It can also tell the computer to test if at least one condition is true, out of any number of conditions more than two.

GOTO This codeword tells the computer not to continue running the program at the next line of code. Instead the computer is told to go to a different line number and continue from there.

The IF and THEN codewords

The IF codeword is a statement/command. The THEN codeword is a statement.

IF is always used with THEN to tell the computer to make a decision and then to act on that decision. To help it make the decision the computer tests something to find out whether or not it is true. If it is true, then the computer does one thing, but if it is not true then the computer does something else (or perhaps nothing at all).

How to use the IF and THEN codewords

Here is a simple IF statement:

80 IF cats = 2 THEN PRINT "I have 2 cats"

This line of the program tells the computer that if the value of the variable cats is 2 then it should display the message I have 2 cats.

You can also use the IF and THEN codewords to tell the computer to test whether or not the value of a variable is 0. Here is an example of how you can do this:

90 IF dogs  THEN PRINT  “I have at least one dog.”

This line of the program tells the computer that if the value of dogs is not zero it should print the message I have at least one dog. But if the value of dogs is 0 then the computer does nothing special – it just continues on to the next line of the program.

How to compare numbers using IF and THEN

In Lesson 3 we learned a couple of ways to test for true or false. Sometimes it is useful to make an IF . . . THEN  statement depend on a true/false test, so that we tell the computer to do something IF the result of the test is true.

Here are some examples. These examples use some symbols that you might already have learned about in your Maths lessons. If you don’t know about them already, now is a good time for you to learn them – they are used to compares two numbers or values.

l > m means l is greater than m
l < m means l is less than m
l > m means l is greater than m or it is equal to m
l < m means l is less than m or l is equal to m
l < > m means l is not equal to m

 

The “greater than” comparison:

50 IF a > b THEN PRINT "a is greater than b"
51 REM In line 50 the test compares the values of the variables a and b.
52 REM We are telling the computer that if a is greater than b then
53 REM it should display the message "a is greater than b".

The “greater than or equal to” comparison:

60 IF a > b THEN PRINT "a is greater than or equal to b"
61 REM Line 60 tells the computer that if a is greater than b,
62 REM or if a is equal to b, then it should display the message.

The “less than” comparison:

70 IF a < b THEN PRINT "a is less than b"
71 REM Line 70 tells the computer that if a is less than b
72 REM the it should display the message.

The “less than or equal to” comparison:

80 IF a < b THEN PRINT "a is less than or equal to b"
81 REM Line 80 tells the computer that if a is less than b
82 REM or if a is equal to b then it should display the message.

The “not equal to” comparison:

90 IF a < > b THEN PRINT "a is not equal to b"
91 REM Line 90 tells the computer that if a is not equal to b
92 REM then it should display the message.

The AND codeword

The AND codeword is a logical operator.

How to use the AND codeword

The AND codeword tells the computer to test if two or more different conditions are true. We use AND together with IF and THEN, to help the computer make slightly more complex decisions than those in the previous examples.

When we are using the AND codeword, if all of the conditions the computer is told about are true then the overall combination of conditions is true. But if one or more of the conditions is not true then the combination of conditions is not true.

Here is an example:

100 cats = 2 AND dogs THEN PRINT "I have 2 cats and at least one dog"

This line of the program tells the computer that if the value of the variable cats is 2, and if the value of the variable dogs is not 0, then it should display the message I have 2 cats and at least one dog.

But if the value of the variable cats is not 2 then the computer won’t display that message. And even if the value of cats is 2, then if the value of dogs is not true then the computer still won’t display that message.

The OR codeword

The OR codeword is a logical operator.

How to use the OR codeword

The OR codeword tells the computer to test if any of two or more different conditions are true. We use OR together with IF and THEN.

When we are using the OR codeword, if any of the conditions the computer is told about are true then the overall combination of conditions is true. But if all of the conditions are not true then the combination of conditions is not true.

Here is an example:

100 IF cats = 2 OR cats = 3 THEN PRINT "I have 2 or 3 cats"

This line of the program tells the computer that if the value of the variable cats is 2, or if it is 3, then it should display the message I have 2 or 3 cats.

But if the value of the variable cats is not 2 or 3, then the computer won’t display that message. With the OR codeword, it is only necessary for one or more of the conditions to be true for the overall condition to be true.

The GOTO codeword

The GO TO codeword is a statement/command.

The GO TO codeword tells the computer to jump to a particular line of the program.

How to use the GO TO codeword

GO TO is very easy to use. Here is an example:

60 GO TO 350

This tells the computer to jump to line 350 of the program and continue running from there.

Example Program

This program plays a simple game of Rock, Scissors and Paper.
Line 800 to 840 asks the user for their choice.Line 850 to 1030 works out who has won and displays a message. Line 1040 to 1080 asks if you want to play again.

800 CLS
810 PRINT "What do you choose?": PRINT : PRINT "Rock (1)": PRINT "Scissors (2)": PRINT "Paper (3)"
820 PRINT : INPUT m
830 IF m>0 AND m<4 THEN GO TO 850
840 PRINT "You must enter a number between 1 and 3": GO TO 820
850 IF c<>m THEN GO TO 870
860 IF m=1 THEN PRINT "We both have Rock we draw": GO TO 1040
861 REM Both players have Rock it's a draw
862 IF m=2 THEN PRINT "We both have Scissors we draw": GO TO 1040
863 REM Both players have Scissors it's a draw
864 PRINT "We both have Paper we draw": GO TO 1040
865 REM Both players have Paper it's a draw
870 IF (c=1) THEN GO TO 930
880 IF (c=2) THEN GO TO 970
890 REM Computer has Paper
895 PRINT "I have Paper ";
896 REM Print the computers choice
900 IF m=1 THEN GO TO 920
910 PRINT "You win ": GO TO 1020
920 PRINT "I Win": GO TO 1030
930 REM Computer has Rock
935 PRINT "I have Rock ";
936 REM Print the computers choice
940 IF m=2 THEN GO TO 960
950 PRINT "You win": GO TO 1030
960 PRINT "I win": GO TO 1010
970 REM Computer has Scissors
975 PRINT "I have Scissors ";
976 REM Print the computers choice
980 IF m=3 THEN GO TO 1000
990 PRINT "You win": GO TO 1010
1000 PRINT "I win": GO TO 1020
1010 PRINT "Rock beats Scissors": GO TO 1040
1020 PRINT "Scissors beats Paper": GO TO 1040
1030 PRINT "Paper beats Rock"
1040 LET c=c+1
1050 IF c=4 THEN LET c=1
1060 INPUT "Do you want to play again? (Y/N)";a$
1070 IF (a$="y") OR (A$="Y") THEN GO TO 800
1080 PRINT : PRINT "That was fun!":PRINT "Thanks for playing"