Switch Statements

Similar to if statements, a switch statement can be used to branch over multiple execution paths.

A switch statement has a condition, which must evaluate to a concrete integer scalar type (such as i32 or u32). The case selectors must have the same type as the condition expression.

Like with if statements, the parentheses around the condition are optional.

A switch statement can have zero or more case blocks.

A default block is strictly required in every switch statement. Multiple default blocks are not permitted.

case and default blocks require curly braces {} around their bodies.

There is no implicit fallthrough in WGSL (no break is needed at the end of a block), but case blocks can specify multiple selectors in a comma-separated list. The default keyword may also be included in a multi-selector list.

Example
let a = 4;
switch a {
  case 1, 2, 3: {
    // Executes if a is 1, 2, or 3
  }
  default: {
    // Executes if none of the above match
  }
}

// Default can be included in a multi-selector list
switch a {
  case 1, 2, default: {
    // Executes for 1, 2, or any unhandled value
  }
}