BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – IF...THEN
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 239
Here, the BASIC Stamp will look for and measure a pulse on I/O pin 0,
then compare the result, value, against 4000. Based on this comparison, a
message regarding the pulse width value will be printed.
If value is greater than 4000, “Value was greater than 4000” is printed to
the screen. Look what happens if value is not
greater than 4000… the code
in the ELSE block is run, which is another IF…THEN…ELSE statement.
This “nested” IF…THEN statement checks if value is equal to 4000 and if it
is, it prints “Value was equal to 4000” or, if it was not equal, the last ELSE
code block executes, printing “Value was less than 4000.” Up to sixteen
(16) IF…THENs can be nested like this.
The nesting option is great for many situations, but, like single-line syntax,
may be a little hard to read, especially if there are multiple nested
statements or there is more than one instruction in each of the Statement(s)
arguments. Additionally, every multi-line IF…THEN construct must end
with ENDIF, resulting in two ENDIFs right near each other in our
example; one for the innermost IF…THEN and one for the outermost
IF…THEN. For this reason, IF…THEN supports an optional ELSEIF
clause. The ELSEIF clause takes the place of a nested IF…THEN and
removes the need for multiple ENDIFs. Our IF…THEN construction from
the example above could be rewritten to:
' {$PBASIC 2.5}
IF (value > 4000) THEN ' evaluate duration
DEBUG "Value was greater than 4000"
ELSEIF (value = 4000) THEN
DEBUG "Value was equal to 4000"
ELSE
DEBUG "Value was less than 4000"
ENDIF
This IF…THEN construct does the same thing as in the previous example:
1) if value is greater than 4000:
it displays “Value was greater than 4000”
2) else, if value is equal to 4000 (the ELSEIF part):
it displays “Value was equal to 4000”
3) and finally (ELSE) if none of the above were true:
it displays “Value was less than 4000”
USING THE ELSEIF CLAUSE