For Statements
A for statement is made up of:
- An optional initializer
- An optional condition
- An optional
continuingexpression - A loop body
The initializer, condition, and continuing expressions are each separated by a ;.
Unlike if and switch statements, a for statement requires parentheses around its loop controls.
Braces are strictly required around the for body.
All loops in WGSL must terminate.
As with other statement conditions, if provided, the condition must be a boolean expression.
The condition is evaluated before executing the body on each iteration.
If the condition is false the loop will end.
A break statement can be used to terminate the closest enclosing loop or switch statement.
Example
A continue statement can be used to skip the rest of the current iteration and proceed to the next iteration (jumping directly to the update/continuing expression of the loop).
No Increment/Decrement or Compound Assignment Operators
Unlike JavaScript, C++, or GLSL, WGSL does not support increment/decrement operators (++, --) or compound assignment operators (+=, -=, *=, /=).
You must always write out the full explicit assignment, for example:
- Use i = i + 1 instead of i++ or i += 1.
- Use counter = counter + 1 instead of counter += 1.
/*
* 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.
*/
fn for_loop() -> u32 {
var counter = 0u;
// Parenthesis are required.
for (var i = 0; i < 10; i = i + 1) {
counter = counter + 1;
}
// Condition, initializer and continue are optional.
var exit = false;
const a = 2;
for (;;) {
if a == 4 {
// Go to the next iteration of the loop.
continue;
}
if exit {
// Break out of the for loop
break;
}
exit = true;
}
return counter;
}