The Beginner's Page
Making Decisions
Richard Mansfield
Assistant Editor
A major distinction between a computer and an ordinary pocket calculator is that the computer can make decisions. It can be programmed to examine something, to test it, and then to react appropriately to the test results. To see how this works, we'll write a program which gives the computer the personality (and the powers) of Madame Mona, a fortune teller.
First, let's see what is available to us. BASIC has only two decision-making words, IF and ON, but that's all we need. IF decides between two alternatives; ON chooses between several. This is like the difference between two types of tests given in school: true-false or multiple-choice.
When A Decision Is Either/Or
When you write a program, you make a list of things for the computer to do and the list is numbered so it knows in what order you want these commands carried out:
10 DIM Q$(200): REM THIS IS FOR ATARI BASIC ONLY 100 PRINT "HELLO, MY NAME IS MADAME MONA" 110 PRINT "WELCOME TO MY CHAMBERS…" 120 PRINT "HOW OLD ARE YOU?" 130 INPUT AGE
So far, the computer is simply introducing itself and getting your age. It will always start out with these same actions whenever this program is run. It hasn't made any decisions yet.
To make decisions, the computer must have some information to work with. Now it has your age, so it can make a decision based on that:
140 IF AGE > 17 THEN GOTO 170 150 PRINT "STATE LAW DOES NOT PERMIT ME TO" 160 PRINT "TELL THE FORTUNE OF ANYONE UNDER 18.": END
The IF…THEN… in line 140 decides between two alternatives: whether to continue on with the program or to end it. Whatever you put after the THEN will happen only if the item between IF and THEN is true.
How does the computer decide what's true and what's false? It tests the item between IF and THEN and leaves itself a flag with a zero or a one "written on it" (on PET/CBM computers, the flag is zero or -1). When it finishes, THEN takes over and looks at the flag. If the flag is a zero, the computer ignores anything after the THEN on the line and goes to the next line (line 150 in this case). Should the flag be true, (1, or, on Commodore computers, -1) THEN performs the tasks listed after it before going on to the next line number.
You can try out these various "truth tests" directly, without even writing a program. Just type: PRINT 5 = 6. This is obviously not true, so your computer should print 0. Type: ? (shorthand for PRINT) 5 > 6. This is a proposal to the computer that five is greater than six. We'll get to the group of symbols called relational (because they show a relation between two numbers) in a minute.
For now, just notice that the computer again says this is false by printing a zero. Type: ? 5 < 6 and you get a true response because five is less than six. (An easy way to remember the less-than < and more-than > symbols is to look at the symbols themselves. They look like what they mean. The wide open side of the symbols points to the larger (more-than) number and the closed, pointed side points to the smaller (less-than) number.)
In our example, the item between IF and THEN in line 140 says that AGE is greater than 17. If someone answers the age question with the number 19, the item is true and the computer will GOTO 170. If AGE is 14, the item is false, so the instructions following THEN are ignored and the program moves on to lines 150 and 160.
IF … THEN decisions are made using the following symbols:
= Equals < Less than < = Less than or equals > More than > = More than or equals <> Does not equal
You can also use the "logic" symbols (AND and OR) to make IF…THEN tests. They allow the computer to test several items at the same time between the IF and the THEN. We could change the program above by typing in three lines differently. In the new version, Mona's ethical position becomes somewhat clearer:
120 PRINT "HOW OLD ARE YOU?": INPUT AGE 130 PRINT "HOW MUCH MONEY DID YOU
BRING MONA?": INPUT CASH 140 IF AGE> 17 OR CASH> = 10 THEN GOTO 170
In this version, as before, line 140 allows the program to continue if the customer is over 17, but it also takes into account any youngsters with ten dollars or more. Here, either age or money can influence Mona's decision. You could type this in directly, too: ? 5 = 6 OR 6 = 6 will give you a true, but ? 5 = 6 AND 6 = 6 will be false because AND expects them both to be correct (OR only needs one of them to be correct).
Selecting Between Several Possibilities
ON…GOTO (or GOSUB) does not test or create flags. It simply expects a number and sends the program off to the lines which follow the GOTO. ON X GOTO 100, 200, 375, 400 means that if X is one, the program will jump to line 100, if X is three, the program starts following the instructions listed on line 375. You can have as many line numbers after X as will fit on one line. If, by some mistake, X is beyond the range of line numbers listed after the GOTO (99447 or 0 or a negative number), the program will just continue on without jumping to any of the lines listed.
170 PRINT "ASK MADAME A QUESTION THAT CAN" 180 PRINT "BE ANSWERED BY YES OR NO…" 190 INPUT Q$ 200 X = INT (RND (1)*5) + 1 210 ON X GOSUB 230, 240, 250, 260, 270 220 PRINT: GOTO 170 230 PRINT "NO" : RETURN 240 PRINT "YES" : RETURN 250 PRINT "MAYBE" : RETURN 260 PRINT "IT IS FAIRLY LIKELY": RETURN 270 PRINT "MONA DOESN'T KNOW. ASK ME LATER" : RETURN
We ask for a random number between 1 and 5 in line 200 which will determine what GOSUB takes effect in line 210. We could have written a series of IF…THEN lines:
210 IF X = 1 THEN GOSUB 230 211 IF X = 2 THEN GOSUB 240
but the ON structure makes things simpler. We can just create a list of choices and let the computer select from them using ON.
To make this program more interesting, you might want to add to Mona's repertoire of answers. Simply put some more lines in and then add them to the list on line 210. Don't forget to change the number five in line 200 to equal the number of answers you've created. RND will then give a random number between one and the total number of possible answers.