Contents / Structure within BASIC / Previous chapter / Next chapter / Index


16 IF... THEN...ELSE and more on TRUE and FALSE

The IF...THEN statement has been used in several of the programs earlier in this book - for example, in the program on page 88 which checked your multiplication. Line 100 was:

IF H=F*G THEN PRINT "CORRECT" ELSE PRINT "WRONG"

As you will realise, this type of statement enables the computer to make a choice as it is working its way through the program. The actual choice that it makes will depend on the value of H, F and G at the time. As a result, the same program can behave in quite different ways in different circumstances.

Multiple line statements

It was explained earlier (page 54) that you cm put more than one statement on a line and this can be particularly useful with the IF...THEN statement. Take, for example,

10 X=4 : Y=6: PRINT "HELLO"

20 PRINT X + Y: X=X+Y: PRINT X+Y

>RUN HELLO

10

16

which is just the same as

10 X=4

20 Y=6

30 PRINT "HELLO"

40 PRINT X+Y

50 X=X+Y

60 PRINT X+Y

This helps to understand how the computer treats multiple statement lines using the IF...THEN statement. In the first example which follows, K=6 and therefore the computer obeys everything after the word THEN until the word ELSE. Note that a colon only separates statements - the word ELSE must be found if you want the other course of action to follow.

10 K=6

20 IF K=6 THEN K=9: PRINT "K WAS 6" ELSE PRINT "K WAS NOT 6": PRINT "END OF LINE"

>RUN

K WAS 6

(Note that line 20 was so long that it overflowed on the printer but it is all part of line 20.)

Changing line 10 to K=7 causes the computer to execute everything after the ELSE and as a result it prints

K WAS NOT 6

END OF LINE

IF...THEN is often used with more complicated conditions involving the words AND, OR and NOT for example

IF X=5 AND Y=6 THEN PRINT "GOOD"

IF X=5 OR Y=6 THEN PRINT "TOO LARGE"

The word NOT reverses the effect of a condition, thus

IF NOT (X=6) THEN PRINT "X NOT 6"

These are powerful features which are easy to use.

For the slightly more advanced

It was explained above that you can use multiple statement lines with IF...THEN but this leads to messy programs. It is far better to use procedures if you want a whole lot of things to occur. Thus:

100 IF H=F*G THEN PROCGOOD ELSE PROCBAD

This helps to keep the program readable which is very important, not just from an aesthetic point of view but from the very practical point that a readable program is much easier to get right!

More on TRUE and FALSE

On page 89 the concept of TRUE and FALSE was introduced. A variable can have a numeric value (e.g. 6 or 15) or it can be TRUE or FALSE. In fact this is just playing with words (or perhaps we should say numbers) since the computer understands TRUE to have the value -1 and FALSE to have the value 0.

10 IF 6=6 THEN PRINT "YES" ELSE PRINT "NO"

>RUN

YES

This prints YES because 6=6 is TRUE

5 H=-1

10 IF H THEN PRINT "YES" ELSE PRINT "NO"

>RUN

YES

The above program prints YES because H is TRUE since it has the value -1.

5 H=0

10 IF H THEN PRINT "YES" ELSE PRINT

"NO"

>RUN

NO

This program sets H = FALSE at line 5 and so the program prints NO. -1 implies TRUE and 0 implies FALSE. What about other values of H? In fact all non-zero values are regarded as TRUE as the following shows.

5 H=-55

10 IF H THEN PRINT "YES" ELSE PRINT "NO"

>RUN

YES

Here are some other peculiar examples

10 G= (6=6)

20 PRINT G

>RUN

-1

because (6=6) is TRUE

10 IF 5-6 THEN PRINT "TRUE"

>RUN

TRUE

This works because (5-6) is -1 which is TRUE

These tricks are more than academic. They can be very useful - not least sometimes in trying to fathom out what on earth the computer thinks it is doing!

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001