Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Switch statements

The switch statement is similiar to that of Java. Unlike in Java, the switch statement does not include fallthroughs.

switch (v) {
    case 0:
    case 1:
        trace("zero or one");
    default:
        trace("other");
}

The switch type statement is used to match the type of a discriminant value.

switch type (v) {
    case (d : double) {
        // double
    }
    default {
        // no matching case
    }
}

Syntax

    SwitchStatement :
      switch ParenListExpression { CaseElementsabbrev }
      switch [no line break] type ParenListExpression { TypeCaseElements }
    CaseElementsω :
      «empty»
      CaseElementω
      CaseElementsfull CaseElementω
    CaseElementω :
      CaseLabel{1,} CaseDirectivesω
    CaseLabel :
      case ListExpressionallowIn :
      default :
    CaseDirectivesω :
      Directiveω
      CaseDirectivesfull Directiveω
    TypeCaseElements :
      «empty»
      TypeCaseElement
      TypeCaseElements TypeCaseElement
    TypeCaseElement :
      case ( TypedPattern ) Block
      default Block