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
/*
* Copyright ©2026 Michael R. Bernstein. All new modifications licensed under Apache 2.0.
* Upstream lineage ©2023 governed by original BSD 3-Clause. See README.md.
*/
const a = 4;
fn switch_case() -> u32 {
switch a {
case 1: {
return 1;
}
// Multiple selectors for one block
case 2, 3: {
return 6;
}
case 4: {
return 4;
}
// Lone default
default: {
return 5;
}
}
}
fn switch_default() -> u32 {
// Parenthesis are optional.
switch (a) {
case 1, 2, 3: {
return 1;
}
// Default mixed with other selectors
case 5, default, 6: {
return 4;
}
}
}