Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF (EN)

84 16 IF...THEN...ELSE 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 in chapter 14 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 values of H, F and G at the time. As a result, the same program can behave in very different ways in different circumstances.

Multiple statement lines It was explained earlier (chapter 7) that you can 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.