IF is the BASIC statement that implements conditional execution.
|Availability||Present in all original versions of BBC BASIC.|
|Syntax||BASIC I-IV|| |
|BASIC V|| |
|Token (hex)||BASIC I-V|| |
|Description||BASIC I-IV|| Evaluates the <testable condition>. If equal to |
Otherwise it executes the first <statement>, skipping everything between
|BASIC V|| As for BASIC I-IV, but "|
|Associated keywords|| |
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.
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 (
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"
FALSE (zero) is the only false value of a <testable
condition>; all others are assumed to be
TRUE (whose actual
value is -1.)
THEN is compulsory immediately before an
GOTO line number, or an assignment to
TIME. (In these cases also
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
IF statements cannot be safely nested, as when a
<testable condition> is
FALSE the interpreter skips to the
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)