How computers compare things (predi? YESTERDAY One of the most useful thingsa computer can do isto compare/-wen tu pieces of information and then do difterent things according | WEAR MY tothe results To do this you use the wordslF. THEN The computer can do several different tests on information to compare it. The symbols for the tests are shown above. lt can test to see iftwo pieces of data are egual, different, or if one is greater or less than the other. Here is a program using IF and THEN.At hne 20 the computer stores the word you input in variable WS. Then, at lines 30 and 40 it checks to see if the word in W$ isthe same as "rain" or "sunny'". Ifitis, it prints 6 5 Ageprogram 10 PRINT "HOW OLD ARE YOU" 20 INPUT A 30 IF A>16 THEN PRINT "OLD" 40 IF A<16 THEN PRINT "YOUNG" 50 IF A-16 THEN PRINT "JUST RIGHT" RUN HOW OLD ARE YOU 216 JUST RIGHT In the age program, the computer compares input A with the bgure 16. Ifitis bigger than 16 it pnints "old". Ifit is smaller |. ipnnts "young" and ifit1s 16 it prints"just | ' MNlProgram puzzle - Can you write a program lo ge! the computer to ask you sums, the narrac" ar cve vou the nght answer? SCARF 2 IF AsB THEN PRINT "THEY ARE EOUAL'" IF A>B THEN PRINT "A IS BIGGER" IF AB THEN PRINT "THEY ARE NOT EOUAL" These lines show how you use the symbols with IF and THEN to make the computer compare two pieces of data. You can compare any kind of data - words, numbers and variables, l.e. the contents of memory spaces, too. out one ofthe responses. l you put ina different word at line 20 nothing will happen. You could change the words in lines 30 and 40, though, then try inputting one of the new words. French lesson 10 PRINT "HOW DO YOU SAY RED IN FRENCH" 20 INPUT A$ 30 IF A$-"ROUGE" THEN PRINT "CORRECT" 40 IF A$<>"ROUGE" THEN PRINT "NO, ROUGE'" RUN HOW DO YOU SAY RED IN FRENCH ?BLEU NO, ROUGE right". ln the other program the computer prints out one of two different responseS depending on whether A$ eguals"rouge" or not. n either print Branching programS IF As6 THEN LET A$-'SIX" IF XeY-—2 THEN LET Z-0 computer to go to ling 30. You can give the computer almost any instruction after the word THEN, as shown above. A useful instruction is to make itgo to another line. (On most computers, but Maths program 10 PRINT "TYPE IN A NUMBER" 20 INPUT A 30 PRINT "TYPE IN ANOTHER NUMBER'" 40 INPUT B 50 PRINT "DO YOU WANT TO" 60 PRINT "ADD, SUBIRACT, MULTIPLY" 66 PRINT "DIVIDE OR STOP" 70 INPUT C$ 80 IF C$-"ADD" THEN PRINT A1B 90 IF C$<"SUBTRACT" THEN PRINT A-B 100 IF C$-"MULTIPLY" THEN PRINT A4B 110 IF C$s"DIVIDE" THEN PRINT A/B 120 IF C$-"STOP" THEN STOP 130 GOTO 10 Age guessing program This program will go on repeating itself until G- 14. When G- l4the computer will pass over lines 30 and 40 and print Pd Ram di NR 7 These two hnes | make the computer S branch to other part (the program not the ZX81, you can leave out the word GOTO.) You usually need a STOP instruction in programs with GOTO, or the computer wil] go on repeating the program endlessly. TME PROGRAM WILL. ONLY STOP WHEN YOU RUN TYPE IN A NUMBER nI TYPE IN ANOTHER NUMBER 7184 DO YOU WANT TO ADD, SUBTRACT, MULTIPLY DIVIDE OR STOP - ? In this program the numbers you type in are stored in A and B and your instructions are stored in C$. At lines 80 to 120the computer compares C$ with five diflerent words, and when it finds the right word, it carrjes out the instruction. lt passes over all the lines which are not true. "correct". Can you alter the program so that it gives you some clues, as shown in the picture on the right? 19