IF

Command: Test condition(s) and execute a block of code if valid (positive logic).

((PUB ┆ PRI))
   IF Condition(s)
   -> IfStatement(s)
   < ELSEIF Condition(s)
   -> ElseIfStatement(s) >…
   < ELSEIFNOT Condition(s)
   -> ElseIfNotStatement(s) >…
   < ELSE
   -> ElseStatement(s) >

  • Condition(s) is one or more Boolean expressions to test.
  • IfStatement(s) is a block of one or more lines of code to execute when the IF's Condition(s) is true.
  • ElseIfStatement(s) is an optional block of one or more lines of code to execute when all the previous Condition(s) are invalid and the ELSEIF's Condition(s) is true.
  • ElseIfNotStatement(s) is an optional block of one or more lines of code to execute when all the previous Condition(s) are invalid and the ELSEIFNOT's Condition(s) is false.
  • ElseStatement(s) is an optional block of one or more lines of code to execute when all the previous Condition(s) are invalid.

Explanation

IF is one of the three major conditional commands (IF, IFNOT, and CASE) that conditionally executes a block of code. IF can optionally be combined with one or more ELSEIF commands, one or more ELSEIFNOT commands, and/or an ELSE command to form sophisticated conditional structures.

IF tests Condition(s) and, if true, executes IfStatement(s). If Condition(s) is false, the following optional ELSEIF Condition(s), and/or ELSEIFNOT Condition(s), are tested, in order, until a valid condition line is found, then the associated ElseIfStatement(s), or ElseIfNotStatement(s), block is executed. The optional ElseStatement(s) block is executed if no previous valid condition lines are found.

A "valid" condition is one that evaluates to TRUE for a positive conditional statement (IF or ELSEIF) or evaluates to FALSE for a negative conditional statement (ELSEIFNOT).

Indention is Critical

IMPORTANT: Indention is critical. The Spin language relies on indention (of one space or more) on lines following conditional commands to determine if they belong to that command or not. To have the Propeller Tool indicate these logically grouped blocks of code on-screen, you can press Ctrl + I to turn on block-group indicators. Pressing Ctrl + I again will disable that feature. See the Propeller Tool Help for a complete list of shortcut keys.

Simple IF Statement

The most common form of the IF conditional command performs an action if, and only if, a condition is true. This is written as an IF statement followed by one or more indented lines of code. For example:

if X > 10                        'If X is greater than 10
  !outa[0]                       'Toggle P0
!outa[1]                         'Toggle P1 

This example tests if X is greater than 10; if it is, I/O pin 0 is toggled. Whether or not the IF condition was true, I/O pin P1 is toggled next.

Since the !outa[0] line is indented from the IF line, it belongs to the IfStatement(s) block and is executed only if the IF condition is true. The next line, !outa[1], is not indented from the IF line, so it is executed next whether or not the IF's Condition(s) was true. Here's another version of the same example:

if X > 10                        'If X is greater than 10
  !outa[0]                       'Toggle P0
  !outa[1]                       'Toggle P1
waitcnt(2_000 + cnt)             'Wait for 2,000 cycles 

This example is very similar to the first, except there are now two lines of code indented from the IF statement. In this case, if X is greater than 10, P0 is toggled then P1 is toggled and finally the waitcnt line is executed. If, however, X was not greater than 10, the !outa[0] and !outa[1] lines are skipped (since they are indented and part of the IfStatement(s) block) and the waitcnt line is executed (since it is not indented; it is not part of the IfStatement(s) block).

Combining Conditions

The Condition(s) field is evaluated as one single Boolean condition, but it can be made up of more than one Boolean expression by combining them with the AND and OR operators; see AND and OR for more information. For example:

if X > 10 AND X < 100            'If X greater than 10 and less than 100 

This IF statement would be true if, and only if, X is greater than 10 and X is also less than 100. In other words, it's true if X is in the range 11 to 99. Sometimes statements like these can be a little difficult to read. To make it easier to read, parentheses can be used to group each sub-condition, such as with the following.

if (X > 10) AND (X < 100)        'If X greater than 10 and less than 100

Using IF with ELSE

The second most common form of the IF conditional command performs an action if a condition is true or a different action if that condition is false. This is written as an IF statement followed by its IfStatement(s) block, then an ELSE followed by its ElseStatement(s) block, as shown below:

if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
else                             'Else, X <= 100
  !outa[1]                       'Toggle P1 

Here, if X is greater than 100, I/O pin 0 is toggled, otherwise, X must be less than or equal to 100, and I/O pin 1 is toggled. This IF...ELSE construct, as written, always performs either a toggle on P0 or a toggle on P1; never both, and never neither.

Remember, the code that logically belongs to the IfStatement(s) or the ElseStatement(s) must be indented from the IF or the ELSE, respectively, by at least one space. Also note that the ELSE must be lined up horizontally with the IF statement; they must both begin on the same column or the compiler will not know that the ELSE goes with that IF.

For every IF statement, there can be zero or one ELSE component. ELSE must be the last component in an IF statement, appearing after any potential ELSEIFs.

Using IF with ELSEIF

The third form of the IF conditional command performs an action if a condition is true or a different action if that condition is false but another condition is true, etc. This is written as an IF statement followed by its IfStatement(s) block, then one or more ELSEIF statements followed by their respective ElseIfStatement(s) blocks. Here's an example:

if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
elseif X == 90                   'Else If X = 90
  !outa[1]                       'Toggle P1 

Here, if X is greater than 100, I/O pin 0 is toggled, otherwise, if X equals 90, I/O pin 1 is toggled, and if neither of those conditions were true, neither P0 nor P1 is toggled. This is a slightly shorter way of writing the following code:

if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
else                             'Otherwise,
  if X == 90                     'If X = 90
    !outa[1]                     'Toggle P1 

Both of these examples perform the same actions, but the first is shorter and is usually considered easier to read. Note that the ELSEIF, just like the ELSE, must be lined up (start in the same column) as the IF that it is associated with.

Each IF conditional statement can have zero or more ELSEIF statements associated with it. Look at the following:

if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
elseif X == 90                   'Else If X = 90
  !outa[1]                       'Toggle P1
elseif X > 50                    'Else If X > 50
  !outa[2]                       'Toggle P2 

We have three conditions and three possible actions here. Just like the previous example, if X is greater than 100, P0 is toggled, otherwise, if X equals 90, P1 is toggled, but if neither of those conditions were true and X is greater than 50, P2 is toggled. If none of those conditions were true, then none of those actions would occur.

There is an important concept to note about this example. If X is 101 or higher, P0 is toggled, or if X is 90, P1 is toggled, or if X is 51 to 89, or 91 to 100, P2 is toggled. This is because the IF and ELSEIF conditions are tested, one at a time, in the order they are listed and only the first condition that is true has its block of code executed; no further conditions are tested after that. This means that if we had rearranged the two ELSEIFs so that the "X > 50" were checked first, we'd have a bug in our code.

We did this below:

if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
elseif X > 50                    'Else If X > 50
  !outa[2]                       'Toggle P2
elseif X == 90                   'Else If X = 90 <-- ERROR, ABOVE COND.
  !outa[1]                       'Toggle P1 <-- SUPERSEDES THIS AND THIS CODE NEVER RUNS 

The above example contains an error because, while X could be equal to 90, the elseif X == 90 statement would never be tested because the previous one, elseif X > 50, would be tested first, and since it is true, its block is executed and no further conditions of that IF structure are tested. If X were 50 or less, the last ELSEIF condition is tested, but of course, it will never be true.

Using IF with ELSEIF and ELSE

Another form of the IF conditional command performs one of many different actions if one of many different conditions is true, or an alternate action if none of the previous conditions were true. This is written as with an IF, one or more ELSEIFs, and finally an ELSE. Here's an example:

if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
elseif X == 90                   'Else If X = 90
  !outa[1]                       'Toggle P1
elseif X > 50                    'Else If X > 50
  !outa[2]                       'Toggle P2
else                             'Otherwise,
  !outa[3]                       'Toggle P3 

This is just like the example above, except that if none of the IF or ELSEIF conditions are true, P3 is toggled.

The ELSEIFNOT Condition

The ELSEIFNOT condition behaves exactly like ELSEIF except that it uses negative logic; it executes its ElseIfNotStatement(s) block only if its Condition(s) expression evaluates to FALSE. Multiple ELSEIFNOT and ELSEIF conditions can be combined in a single IF conditional command, in any order, between the IF and the optional ELSE.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.