IF

From BeebWiki
Jump to: navigation, search

IF is the BASIC statement that implements conditional execution.

IF
Availability Present in all original versions of BBC BASIC.
Syntax BASIC I-IV IF <testable condition> [[THEN] <statement>|THEN <num-const>] [ELSE <statement>|<num-const>]
BASIC V IF <testable condition> [[THEN] <statement>|THEN <num-const>] [ELSE <statement>|<num-const>]
IF <testable condition> THEN
[<statement>]
[ELSE
<statement>]
ENDIF
Token (hex) BASIC I-V E7 (statement)
Description BASIC I-IV Evaluates the <testable condition>. If equal to FALSE the interpreter skips to the first ELSE token or the end of the line and continues execution.
Otherwise it executes the first <statement>, skipping everything between ELSE and the end of the line (if ELSE is present) and continues execution.
THEN <num-const> and ELSE <num-const> respectively do a conditional and counter-conditional GOTO to that line number, which is tokenised at input time.
BASIC V As for BASIC I-IV, but "IF <testable condition> THEN" (on its own line) starts a multi-line IF statement, which is only ended by ENDIF and can be safely nested.
Associated keywords THEN, ELSE, ENDIF, TRUE, FALSE

Description

IF is an essential statement to vary the behaviour of a program in response to some condition derived from the user's input, the contents of a file or the system environment.

Unlike REPEAT and WHILE, which may execute their clause several times, the clause(s) of an IF statement are executed at most once each time the interpreter enters the statement.

The simpler form of IF is used in conditional execution: the clause is only carried out if a given condition is true at that time. The condition is specified as a Boolean expression that is freshly evaluated at each encounter.

IF POINT(X,Y) = 2 THEN PROCskid

As part of a bigger loop (not shown), for instance in an arcade game, this line causes an additional action PROCskid whenever the coordinate pair (X,Y) encounters a green part of the screen. The clause statement need not return, of course:

INPUT guess$
IF guess$ = "STOP" THEN END

The longer form of IF is used in alternative execution: there are two clauses, exactly one of which is executed depending on whether the condition is true or false.

IF value < 0 THEN PRINT "Negative" ELSE PRINT "Non-negative"

Notes

FALSE (zero) is the only false value of a <testable condition>; all others are assumed to be TRUE (whose actual value is -1.)

Use of THEN is compulsory immediately before an implied-GOTO line number, or an assignment to EXT, HIMEM, LOMEM, PAGE, PTR or TIME. (In these cases also LET is forbidden.)

Without a THEN or ELSE clause, IF does nothing and can be used to discard a <numeric> result from a function, instead of assigning to a dummy variable; however this usage should only appear at the end of a line.

IF GET

IF EVAL("FN"+switch$)

Single line IF syntax

Single line IF statements cannot be safely nested, as when a <testable condition> is FALSE the interpreter skips to the first ELSE token, regardless of which <code>IF</code> statement it belongs to. The schema

IF A THEN
  IF B THEN
    PROCfred
  ELSE
    PROCjim
  ENDIF
ELSE
  PROCsheila
ENDIF

would have to be written as

IF A THEN IF B THEN PROCfred ELSE IF A THEN PROCjim ELSE PROCsheila

(In this case, it would be equivalent and faster to write:)

IF A AND B THEN PROCfred ELSE IF A THEN PROCjim ELSE PROCsheila


As a result of this the schema

IF C THEN PROChazel
PROCandy

could be represented by:

IF C THEN PROChazel:IF FALSE ELSE PROCandy

although in 6502 BASIC this saves no space even when fully crunched.

-- beardo 14:53, 21 January 2008 (UTC)