BASIC stamp manual v2.2
IF…THEN – BASIC Stamp Command Reference
Page 238 • BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com
The IF…THEN in the previous example is called a “single-line” syntax. It
is most useful where you only have one instruction to run as the result of a
Condition. Sometimes this form may be a little hard to read, like in our
above example. For these cases, it would be better to use the “multi-line”
syntax of IF…THEN. The multi-line format allows the same flexibility as
the single-line format, but is easier to read in most cases and requires an
ENDIF statement at the end. For example, our IF…THEN line above
could be re-written as:
IF (x // 2) = 0 THEN
DEBUG " EVEN" ' even number
ELSE
DEBUG " ODD" ' odd number
ENDIF
This example runs exactly the same way, is much easier to read and also
leaves extra room to add some helpful comments on the right. We also
indented the Statement(s) for clarity and suggest you do the same.
Did you notice that multi-line syntax requires ENDIF to mark the end of
the IF…THEN…ELSE construct? That is because the Statement(s)
argument can be multiple instructions on multiple lines, so without
ENDIF there is no way to know just where the IF…THEN…ELSE ends.
Occasionally, it may be necessary to have compound IF statements. One
way to achieve this is through nested IF…THEN…END constructs:
' {$PBASIC 2.5}
value VAR Word
DO
PULSIN 0, 1, value ' measure pulse input
DEBUG DEC value, CR
IF (value > 4000) THEN ' evaluate duration
DEBUG "Value was greater than 4000"
ELSE
IF (value = 4000) THEN
DEBUG "Value was equal to 4000"
ELSE
DEBUG "Value was less than 4000"
ENDIF
ENDIF
DEBUG CR, CR
PAUSE 1000
LOOP
S
INGLE-LINE VS. MULTI-LINE IF…THENS
N
ESTED IF…THENS