case

Syntax case expression

  expr1

    statements1

  expr2

    statements2

  exprN

    statementsN

else

  statements

Action Evaluates the case expression, and then evaluates item expressions 1 through N in succession until one of the resulting values matches that of the case expression. When a matching value is found, the corresponding statements are executed, and the case statement is exited.

The statements under each case item expression are optional; if they are omitted, then the next statement list that appears in the case body is associated with that item.

The else clause of the case statement is optional. If it is provided, and none of the item expressions in the main case body match the case expression, the else statements are executed.

Examples case dialog.yesNoCancel ("Save changes before quitting?")
  1

    msg ("Your changes are being saved...")

    fileMenu.save ()

  2

    msg ("Your changes are being discarded...")

  3

    msg ("Ok, we won't quit")

    return (false) // don't continue process

fileMenu.quit ()
Type this script into a script window. In this example, dialog.yesNoCancel is known to return a result of 1, 2 or 3. The case statement allows the appropriate action to be taken for each response.

case user.initials
  "DW"

    msg ("Hi Dave!")

  "dmb"

    msg ("Yo Doug!")

else
  msg ("Howdy stranger.")

Type this script into a script window. This example demonstrates the use of the else clause, and also illustrates a feature of the case statement that is unlike most other languages: the case expression and the item expressions can work with values of any type.

case itemhit
  1 // user clicked OK

    copyfromdialog ()

    return (false)

  2 // user clicked Cancel

    return (false)

  3

  5

    checkboxhit (itemhit)

  6

  7

  8

    radiobuttonhit (itemhit)

This is just a script fragment, so don't attempt to execute it. It shows how the same statements can be shared by several item expressions by listing them in sequence and including the statements only under the last item.

Notes The item expressions do not have to be simple constants; they may be complex expressions if desired.

Once an item expression's value matches that of the case expression, none of the subsequent expression are evaluated.

See Also if

Discuss