The CASE statement selects one, and only one, statement or block of statements for execution, depending on the value of an expression. This expression is called the "case-selector" expression. Each statement that is part of a CASE statement is preceded by a "clause" expression that is compared to the value of the case-selector expression. CASE executes by converting the clause expression to the data type of the case-selector, and then comparing the two expressions. If a match is found, the statement is executed and control resumes directly below the CASE statement. This is different that SWITCH, which falls through and continues to execute any remaining statements.

The ELSE clause of the CASE statement is optional. If included, it matches any selector expression, causing its code to be executed. For this reason, it is usually written as the last clause in the CASE statement. The ELSE statement is executed only if none of the preceding statement expressions match. If an ELSE clause is not included and none of the values match the selector, an error occurs and program execution stops.

The BREAK statement can be used within CASE statements to force an immediate exit from the CASE.

In this CASE statement, only one clause is selected, and that clause is the first one whose value is equal to the value of the case-selector expression.

Tip: Each clause is tested in order, so it is most efficient to order the most frequently selected clauses first.

For more information on the difference between CASE and SWITCH, see CASE Versus SWITCH.

Note: Before doing the comparison, IDL will automatically convert the clause expression to the data type of the case-selector expression. This may lead to unexpected results if you try to compare strings to numbers, or floating-point values to integers.

Examples


This example illustrates how the CASE statement, unlike SWITCH, executes only the one statement that matches the case expression:

x=2
 
CASE x OF
   1: PRINT, 'one'
   2: PRINT, 'two'
   3: PRINT, 'three'
   4: PRINT, 'four'
   ELSE: PRINT, 'Not one through four'
ENDCASE

IDL Prints:

two

For more examples, please see Additional Examples near the bottom of this topic.

Syntax


CASE expression OF

    expression: statement(s)

    ...

    expression: statement(s)

[ ELSE: statement(s) ]

ENDCASE

Additional Examples


More than one statement can be executed when a match for the case selector expression is encountered. To execute multiple statements, surround the statements with BEGIN...END statements:

x=5
 
CASE x OF
   1: PRINT, 'one'
   2: PRINT, 'two'
   3: PRINT, 'three'
   4: PRINT, 'four'
   ELSE: BEGIN
      PRINT, 'You entered: ', x
      PRINT, 'Please enter a value between 1 and 4'
      END
ENDCASE

Another example shows the CASE statement with the number 1 as the selector expression of the CASE. One is equivalent to true and is matched against each of the conditionals.

CASE 1 OF
   (X GT 0) AND (X LE 50): Y = 12 * X + 5
   (X GT 50) AND (X LE 100): Y = 13 * X + 4
   (X LE 200): BEGIN
     Y = 14 * X - 5
     Z = X + Y
   END
ELSE: PRINT, 'X has an illegal value.'
ENDCASE

In this CASE statement, only one clause is selected, and that clause is the first one whose value is equal to the value of the case selector expression.

Tip: Each clause is tested in order, so it is most efficient to order the most frequently selected clauses first.

Version History


Original

Introduced

See Also


BEGIN...END, BREAK, Case vs Switch, CONTINUE, FOR, FOREACH, GOTO, IF...THEN...ELSE, REPEAT...UNTIL, SWITCH, WHILE...DO

IDL Programming