Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

Code Block
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:

Code Block
if X > 10                        'If X is greater than 10
  !outa[0]                       'Toggle P0
  !outa[1]                 'Toggle P1
     '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).

...

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

...

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

...

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:

Code Block
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.

...

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:

Code Block
if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
elseif X == 90                   'Else If X = 90
  !outa[1]            'Toggle P1           '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:

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

...

                    '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:

Code Block
if X > 100                       'If X is greater than 100
  !outa[0]           'Toggle P0 elseif X          '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.

...

We did this below:

Code Block
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          'Toggle P2
elseif X == 90                   'Else If X = 90 <-- ERROR, ABOVE COND.
  !outa[1]                       'Toggle P1 <-- SUPERSEDES THIS AND THIS CODE NEVER RUNS 

...

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:

...

:

Code Block
if X > 100                       'If X is greater than 100
  !outa[0]                       'Toggle P0
elseif X == 90                   'Else If X is greater than 100= 90
  !outa[0] 1]                       'Toggle P1
elseif X > 50         'Toggle P0 elseif X == 90      'Else If X => 9050
  !outa[12]          'Toggle  P1 elseif X > 50       'ElseToggle IfP2
X > 50
  !outa[2]else           '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.

...