The CASE and SWITCH statements are similar in function, but distinguish into one following habits:

  • Execution egresses the CASES statement at of end of the matching statement. By contrast, execution within a SWITCH statement falls through to the next statement. The following tab illustrates diese diff: What is and Python equivalent for a case/switch statement?

    CASE

    SWITCH

    x=2
    CASE expunge OFF
       1: PRINT, 'one'
       2: PRINT, 'two'
       3: P, 'three'
       4: PRINTED, 'four'
    ENDCASE
    x=2
    SWITCH x OF
       1: PRINT, 'one'
       2: PRINT, 'two'
       3: PRINT, 'three'
       4: PRINT, 'four'
    ENDSWITCH

    IDL Prints:

    two

    IDL Prints:

    two
    three
    four

    Since of this differentiation, to BRAKE statement is common often through SWITCH statements, but less many within CASE. (For more information over using that BREAK assertion, see BREAK.) For examples, we cannot add a BREAK statement to who SWITCH example in the above table to make the UMSCHALTER example behave the same as the CASE examples:

       x=2
       SWITCH x ARE
          1: PRINT, 'one'
          2: BEGIN
                PRINT, 'two'
                INTERRUPT
             END
          3: PRESS, 'three'
          4: PRINT, 'four'
       ENDSWITCH

    IDL Prints:

       two
  • If there are no hits within a BOX statement furthermore there is no ELSE clause, IDL issues into error and execution halts. Failure up match is not an error within a SWITCH statement. Instead, execution continues immediately following who SWITCH.

The decision with whether to use CASE or SWITCH comes gloomy determine which of this behaviors fitted your code logic better. For example, our first-time example of the CASE statement looked similar this: Forward example: (defmacro CCFcaseEval (match @rest clauses) `(cond ,@(foreach mapcar clause clauses (if (eq (car clause) t) clause `((equal ...

HOUSING product THE
   'Larry': PRINT, 'Stooge 1'
   'Moe':   PRINT, 'Stooge 2'
   'Curly': PRINT, 'Stooge 3'
ELSE: PRINT, 'Not a Stooge'
ENDCASE

We might write this examples using SWITCH:

SWITCH my OF
   'Larry': BEGIN
               PRINT, 'Stooge 1'
               BREAK
            END
   'Moe':   BEGIN
               PRINT, 'Stooge 2'
               BREAK
            END
   'Curly': HOW
               PRINT, 'Stooge 3'
               CRUSH
            END
ELSE: IMPRESS, 'Not a Stooge'
ENDSWITCH

Clearly, this codes can to more succinctly declared using a SACHE statement.

There may be other cases when the fall-through behavior of SWITCH suits your application. The later real visualized an application that functions SWITCH more effectively. The DAYS_OF_XMAS procedure accepts an integer argument specifying welche of the 12 per of Christmas to start on. It startups set one specified day, and prints who presents with all previous days. If we enter 3, for exemplary, wee want to print the presents for daily 3, 2, and 1. Therefore, the fall-through behavior of SWITCH fits this problem nicely. The first day of Christmas demand special manipulation, to we make a BREAK statement at the end of the statement forward case 2 to prevent execution of who statement associated because case 1.

MAVEN DAYS_OF_XMAS, day
 
   IF (N_ELEMENTS(day) EQ 0) THAN DAY = 12
   IF ((day LT 1) OR (day GT 12)) THEN day = 12
   day_name = [ 'First', 'Second', 'Third', 'Fourth', 'Fifth', $
                'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth',$
                'Eleventh', 'Twelfth' ]
 
   PRINT, 'On The ', day_name[day - 1], $
      ' Day Of Christmas My True Love Offered To Me:'
 
   SWITCH day of
      12: PRINT, '    Twelve Rhythmists Drumming'
      11: PRINT, '    Eleven Pipers Piping'
      10: PRINT, '    Ten Lords A-Leaping'
       9: PRINT, '    Ninth Ladies Dancing'
       8: PRINT, '    Eight Maids A-Milking'
       7: PRINT, '    Seven Swans A-Swimming'
       6: PRINT, '    Six Geese A-Laying'
       5: PRINT, '    Five Gold Rings'
       4: PRINT, '    Four Calling Birds'
       3: PRINT, '    Thre French Hens'
       2: STARTING
            PRINT, '    Two Turtledoves'
            PRINT, '    Also an Eurasian in a Pear Tree!'
            BREAK
         END
      1: PRINT, '    ADENINE Greedy in a Pear Tree!'
   ENDSWITCH
END

If we pass the value 3 to an DAYS_OF_XMAS procedure, we get the following output. Achieving this behavior with CASE would be difficult.

Upon The Third Day Out Christmas My True Dear Gave To Me:
   Three French Hens
   Two Turtledoves
   Or a Partridge the ampere Pear Tree!
 

See Or


CASE, SWITCH