For Statements

A for statement is made up of:

  • An optional initializer
  • An optional condition
  • An optional continuing expression
  • A loop body

The initializer, condition, and continuing expressions are each separated by a ;.

Example
for (var i = 0; i < 10; i = i + 1) {
    // loop body
}

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
for (;;) {
  break;
}
for (;;) {
  const a = 1;
  switch (a) {
    case 1, default: {
      // Breaks the switch, but not the for loop
      break;
    }
  }
}

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).

Example
for (var i = 0; i < 10; i = i + 1) {
  if (i == 1) {
    continue;
  }
}

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.