Difference between revisions of "IF"
m (1 revision) |
m (Slight formatting changes.) |
||
(One intermediate revision by one other user not shown) | |||
Line 29: | Line 29: | ||
== Description == | == Description == | ||
− | <code>IF</code> 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. | + | <code>IF</code> 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 <code>[[REPEAT]]</code> and <code>[[WHILE]]</code>, which may execute their ''clause'' several times, the clause(s) of an <code>IF</code> statement are executed at most once each time the interpreter enters the statement. | + | Unlike <code>[[REPEAT]]</code> and <code>[[WHILE]]</code>, which may execute |
+ | their ''clause'' several times, the clause(s) of an <code>IF</code> | ||
+ | statement are executed at most once each time the interpreter enters the | ||
+ | statement. | ||
− | The simpler form of <code>IF</code> is used in ''conditional execution'': the clause is only carried out ''if'' a given condition is ''true'' at that time. | + | The simpler form of <code>IF</code> 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 | IF POINT(X,Y) = 2 THEN PROCskid | ||
− | As part of a bigger loop (not shown) in an arcade game, this line causes an ''additional'' action <code>PROCskid</code> whenever the coordinate pair (<code>X</code>,<code>Y</code>) encounters a green part of the screen. | + | As part of a bigger loop (not shown), for instance in an arcade game, this |
+ | line causes an ''additional'' action <code>PROCskid</code> whenever the | ||
+ | coordinate pair (<code>X</code>,<code>Y</code>) encounters a green part of | ||
+ | the screen. The clause statement need not return, of course: | ||
INPUT guess$ | INPUT guess$ | ||
IF guess$ = "STOP" THEN END | IF guess$ = "STOP" THEN END | ||
− | The longer form of <code>IF</code> is used in ''alternative execution'': there are two clauses, exactly one of which is executed depending on whether the condition is ''true'' or ''false''. | + | The longer form of <code>IF</code> is used in ''alternative execution'': |
− | IF value | + | 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 == | == Notes == | ||
− | <code>FALSE</code> (zero) is the only ''false'' value of a <testable condition>; all others are assumed to be <code>TRUE</code> (whose actual value is -1.) | + | <code>FALSE</code> (zero) is the only ''false'' value of a <testable |
+ | condition>; all others are assumed to be <code>TRUE</code> (whose actual | ||
+ | value is -1.) | ||
− | Use of <code>THEN</code> is compulsory immediately before an implied-<code>GOTO</code> line number, or an [[=|assignment]] to <code>EXT</code>, <code>HIMEM</code>, <code>LOMEM</code>, <code>PAGE</code>, <code>PTR</code> or <code>TIME</code>. (In these cases also <code>LET</code> is forbidden.) | + | Use of <code>THEN</code> is compulsory immediately before an |
+ | implied-<code>GOTO</code> line number, or an [[=|assignment]] to | ||
+ | <code>EXT</code>, <code>HIMEM</code>, <code>LOMEM</code>, <code>PAGE</code>, | ||
+ | <code>PTR</code> or <code>TIME</code>. (In these cases also <code>LET</code> | ||
+ | is forbidden.) | ||
− | Without a <code>THEN</code> or <code>ELSE</code> clause, <code>IF</code> 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. | + | Without a <code>THEN</code> or <code>ELSE</code> clause, <code>IF</code> |
+ | 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 GET | ||
Line 53: | Line 75: | ||
== Single line IF syntax == | == Single line IF syntax == | ||
− | Single line <code>IF</code> statements cannot be safely nested, as when a <testable condition> is <code>FALSE</code> the interpreter skips to the ''first'' <code>ELSE</code> token, regardless of which <code | + | Single line <code>IF</code> statements cannot be safely nested, as when a |
+ | <testable condition> is <code>FALSE</code> the interpreter skips to the | ||
+ | ''first'' <code>ELSE</code> token, regardless of which <code>IF</code> | ||
+ | statement it belongs to. The schema | ||
IF A THEN | IF A THEN | ||
IF B THEN | IF B THEN | ||
Line 74: | Line 99: | ||
''could'' be represented by: | ''could'' be represented by: | ||
IF C THEN PROChazel:IF FALSE ELSE PROCandy | IF C THEN PROChazel:IF FALSE ELSE PROCandy | ||
− | although in 6502 BASIC this saves no space even when fully [[Crunching BASIC programs|crunched]]. | + | although in 6502 BASIC this saves no space even when fully |
+ | [[Crunching BASIC programs|crunched]]. | ||
-- [[User:Beardo|beardo]] 14:53, 21 January 2008 (UTC) | -- [[User:Beardo|beardo]] 14:53, 21 January 2008 (UTC) | ||
[[Category:BASIC keywords]] | [[Category:BASIC keywords]] |
Latest revision as of 17:00, 12 November 2017
IF is the BASIC statement that implements conditional execution.
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)