CASE

Command: Compare expression against matching expression(s) and execute code block if match found.

((PUBPRI))
   CASE CaseExpression
      -> MatchExpression :
        -> Statement(s)
   < -> MatchExpression :
        ->Statement(s) >
   < -> OTHER :
        -> Statement(s)
>

  • CaseExpression is the expression to compare.
  • MatchExpression is a singular or comma-delimited set of value- and/or range-expressions, to compare CaseExpression against. Each MatchExpression must be followed by a colon (:).
  • Statement(s) is a block of one or more lines of code to execute when the CaseExpression matches the associated MatchExpression. The first, or only, statement in Statement(s) may appear to the right of the colon on the MatchExpression line, or below it and slightly indented from the MatchExpression itself.

Explanation

CASE is one of the three conditional commands (IF, IFNOT, and CASE) that conditionally executes a block of code. CASE is the preferred structure to use, as opposed to IF..ELSEIF..ELSE, when you need to compare the equality of CaseExpression to a number of different values.

CASE compares CaseExpression against the values of each MatchExpression, in order, and if a match is found, executes the associated Statement(s). If no previous matches were found, the Statement(s) associated with the optional OTHER command are executed.

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.

Using CASE

CASE is handy where one of many actions needs to be performed depending on the value of an expression. The following example assumes A, X and Y are variables defined earlier.

case X+Y                       'Test X+Y
  10, 15  :  !outa[0]          'X+Y = 10 or 15? Toggle P0
  A*2     :  !outa[1]          'X+Y = A*2? Toggle P1
  30..40  :  !outa[2]          'X+Y in 30 to 40? Toggle P2
X += 5                         'Add 5 to X 

Since the MatchExpression lines are indented from the CASE line, they belong to the CASE structure and are executed based on the CaseExpression comparison results. The next line, X += 5, is not indented from CASE, so it is executed regardless of the CASE results.

This example compares the value of X + Y against 10 or 15, A*2 and the range 30 through 40. If X + Y equals 10 or 15, P0 is toggled. If X + Y equals A*2, P1 is toggled. If X + Y is in the range 30 through 40, inclusive, then P2 is toggled. Whether or not any match was found, the X += 5 line is executed next.

Using OTHER

The optional OTHER component of CASE is similar to the optional ELSE component of an IF structure. For example:

case X+Y                       'Test X+Y
  10, 15  :  !outa[0]          'X+Y = 10 or 15? Toggle P0
  25      :  !outa[1]          'X+Y = 25? Toggle P1
  20..30  :  !outa[2]          'X+Y in 20 to 30? Toggle P2
  OTHER   :  !outa[3]          'Otherwise toggle P3
X += 5                         'Add 5 to X 

This example is similar to the last one except that the third MatchStatement checks for the range 20 to 30 and there's an OTHER component. If X + Y does not equal 10, 15, 25, or is not in the range 20 to 30, the Statement(s) block following OTHER is executed. Following that, the X += 5 line is executed.

There is an important concept to note about this example. If X + Y is 10 or 15, P0 is toggled, or if X + Y is 25, P1 is toggled, or if X + Y is 20 to 30, P2 is toggled, etc. This is because the MatchExpressions are checked, one at a time, in the order they are listed and only the first expression that is a match has its block of code executed; no further expressions are tested after that. This means that if we had rearranged the 25 and 20..30 lines, so that the range of 20..30 is checked first, we'd have a bug in our code. We did this below:

case X+Y                       'Test X+Y
  10, 15  :  !outa[0]          'X+Y = 10 or 15? Toggle P0
  20..30  :  !outa[2]          'X+Y in 20 to 30? Toggle P2
  25      :  !outa[1]          'X+Y = 25? Toggle P1 <-- THIS NEVER RUNS

The above example contains an error because, while X + Y could be equal to 25, that match expression would never be tested since the previous one, 20..30 would be tested first, and since it is true, its block is executed and no further match expressions are checked.

Variations of Statement(s)

The above examples only use one line per Statement(s) block, but each block can be many lines of course. Additionally, the Statement(s) block may also appear below, and slightly indented from, the MatchExpression itself. The following two examples show these variations.

case A                         'Test A
  4       :  !outa[0]          'A = 4? Toggle P0
  Z+1     :  !outa[1]          'A = Z+1? Toggle P1
             !outa[2]          'And toggle P2
  10..15  :  !outa[3]          'A in 10 to 15? Toggle P3 
case A                         'Test A
  4:                           'A = 4?
    !outa[0]                   'Toggle P0
  Z+1:                         'A = Z+1?
    !outa[1]                   'Toggle P1
    !outa[2]                   'And toggle P2
  10..15:                      'A in 10 to 15?
    !outa[3]                   'Toggle P3 

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