Sinclair QL Beginner's Manual page 65

Hide thumbs Also See for QL:
Table of Contents

Advertisement

CHAPTER 10 – LOGIC
If you have read previous chapters you will probably agree that repetition, decision making and
breaking tasks into sub-tasks are major concepts in problem analysis, program design and encoding
programs. Two of these concepts, repetition and decision making, need logical expressions such as
those in the following program lines:
IF score = 7 THEN EXIT throws
IF suit = 3 THEN PRINT "spades"
The first enables EXIT from a REPeat loop. The second is simply a decision to do something or not. A
mathematical expression evaluates to one of millions of possible numeric values. Similarly a string
expression can evaluate to millions of possible strings of characters. You may find it strange that
logical expressions, for which great importance is claimed, can evaluate to one of only two possible
values: true or false.
In the case of
score = 7
this is obviously correct. Either score equals 7 or it doesn't! The expression must be true or false -
assuming that it's not meaningless. It may be that you do not know the value at some time, but that
will be put right in due course.
You have to be a bit more careful of expressions involving words such as OR, AND, NOT but they are
well worth investigating - indeed, they are essential to good programming. They will become even
more important with the trend towards other kinds of languages based more on precise descriptions
of what you require rather than what the computer must do.
AND
The word AND in SuperBASIC is like the word 'and' in ordinary English. Consider the following
program.
100 REMark AND
110 PRINT "Enter two values" \ "1 for TRUE or 0 for FALSE"
120 INPUT raining, hole_in_roof
130 IF raining AND hole_in_roof THEN PRINT "Get wet"
As in real life, you will only get wet if it is raining and there is a hole in the roof. If one (or both) of the
simple logical variables
is false then the compound logical expression
is also false. It takes two true values to make the whole expression true. This can be seen from the
rules below. Only when the compound expression is true do you get wet.
raining
FALSE
FALSE
TRUE
TRUE
raining
hole_in_roof
raining AND hole_in_roof
hole_in_roof
raining and hole_in_roof
FALSE
TRUE
FALSE
TRUE
effect
FALSE
DRY
FALSE
DRY
FALSE
DRY
TRUE
WET

Advertisement

Table of Contents
loading

Table of Contents