Download Z88 USER - Virtual TR-DOS

Transcript
10 FOR a =7 TO 12
20 PRINT a ;
30 IF a < 10 THEN PRINT " This is
young enough";
40 IF a > 9 THEN PRINT " This is
too old";
50 PRINT
60 NEXT a
Incidentally, did you notice that the printouts were not aligned vertically? This is
because 7,8,9 are single digits but 10,11
and 12 have double digits. This can easily
be corrected by using the TAB function.
Change line 20 to read:
20 PRINT a; TAB 3;
Now, let us play Guess the number game
(for 2 players)10 REM Guess the number
20 INPUT "Enter a number", a : CLS
30 INPUT "Guess the number", b
40 IF b = a THEN PRINT "That is
correct ": STOP
50 IF b < a THEN PRINT "That is
too small,try again"
60 IF b > a THEN PRINT "That is
too big, try again"
70 GOTO 30
Note. CLS in line 20 clears the screen to
prevent the other person from seeing the
secret number.
'IF....THEN may used with other instructions other than PRINT, and one of the
most common uses is to make an Unconditional Jump to another line, an example of which is shown below:
10 REM Count to seven
20 LET counter = 1
30 PRINT counter
40 Let counter = counter + 1
50 IF counter <= 7 THEN GOTO
30
MORE DECISIONS
There is a feature of Spectrum BASIC,
which not all computers have. There are
the special expressions known as logical
operators. These are OR, AND, NOT. From
these we get a result which is once again
TRUE or FALSE; for example in
30 IF i < 10 OR j >20 THEN GO TO 66
The meaning of OR is something OR
something
is TRUE if either of the somethings are
TRUE. This includes the case where both
somethings are TRUE.
When the AND operator is used, this
means something AND
something is
TRUE only when both somethings are
TRUE.
Finally, when the NOT operator is used,
this means NOT something is TRUE when
the something is FALSE and vice versa
I hope that this doesn't sound too much
like Double-Dutch, but I believe that if you
think about it carefully it will become clear
to you. A good example of AND, NOT and
OR can be found with the program MAKE
A CHIP.
A truth table is a helpful thing to explain this
more clearly. If we set Logic 1 = TRUE and
logic 0 = FALSE then we get the following
relationships:
OR FUNCTION
AND
FUNCTION
NOT FUNCTION
0 OR O = 0
0 AND 0
NOT 0 = 1
1 OR 0 = 1
1 AND 0
NOT 1 = 0
0 OR 1 = 1
0 AND 1 = 0
1 OR 1 = 1
1 AND 1 = 1
KB.
=
0
=
0