# The zig doc treats \`i += 1\` as an expression, but is it?

**URL:** <https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120>\
**Category:** Help\
**Tags:** language\
**Created:** [October 27, 2026, 6:09pm UTC](https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120 "2026-10-27T18:09:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 27, 2026, 6:09pm UTC](https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120/1 "2026-10-27T18:09:21Z")

</div>

> While loops support a continue expression which is executed when the loop is continued. The **`continue`** keyword respects this expression.
> 
> …
> 
> ```zig
> const expectEqual = @import("std").testing.expectEqual;
> const expect = @import("std").testing.expect;
> 
> test "while loop continue expression" {
> var i: usize = 0;
> while (i < 10) : (i += 1) {}
> try expectEqual(10, i);
> }
> 
> ```

Here, the doc says `(i += 1)` is a continue expression.

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [October 27, 2026, 6:11pm UTC](https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120/2 "2026-10-27T18:11:46Z")

</div>

I think (although I don’t know) that this is a typo. “continue statement” would be more precise, since the statement `i += 1;` does not produce a value in the way that an expression can. that being said, it might be true that even now i am mixing up what Zig means by “expression” vs “statement”.

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [October 27, 2026, 8:01pm UTC](https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120/3 "2026-10-27T20:01:14Z")

</div>

I think it’s correct to say that the continue expression _is_ an expression, but the expression must return `void` so in practice it’s more like a statement. In Zig, the line between a statement and an expression isn’t as clear as in other languages.

As for assignments, the grammar refers to expressions like `a = b` or `a += b` as “assignment expressions”, but unlike other non-assignment expressions like `a + b` they are a special case and are only allowed in specific contexts, one of them being the continue expression.

```txt
WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr?

WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN

```

The reason for these restrictions is probably originally because languages like C that allow nesting assignments within other expressions, such as `a = b += c` or `if (a = b)`, make it easy to make mistakes or write confusing code, and Zig wanted to disarm that footgun.

However, in Zig, an assignment expression returns `void` instead of the assigned value, so these confusing situations couldn’t occur in the first place. Therefore, there is an accepted proposal to turn assignment expressions into proper first-class expressions and allow them everywhere a `void`-returning expression is allowed:

> <https://github.com/ziglang/zig/issues/11805>
>
> Currently the following code snippets which look valid do not parse:
> \`\`\`zig
> te…st {
> var x: u32 = 0;
> while (true) if (true) x += 1;
> 
> var y = false;
> switch (y) {
> true =\> if (true) y = false,
> false =\> if (true) y = true,
> }
>    
> defer if (true) y = false;
> }
> \`\`\`
> Some of these examples are mentioned in #5731 and #3749.
> 
> This is because the grammar does not treat assignment operations such as \`x = 1\` or \`y += 5\` as expressions and only allows them in specific contexts.
> 
> I propose changing the grammar to make assignment operations normal expressions, \[example patch here\](https://github.com/ifreund/zig-spec/commit/3e16dca8045d80d4db327741e3eb6580e4ff517e). I think this may unlock some further cleanups to the grammar as well not included in that example patch.
> 
> Assignment expressions would be defined to always evaluate to type/value void. In effect, we already allow expressions with exactly these semantics such as \`{ x = 1; }\`. This proposal merely makes the grammar a bit more consistent and allows writing more intuitive code as the examples above demonstrate.
> 
> \<details\>
> \<summary\>Original additional proposal to disallow void expressions in most contexts\</summary\>
> That change alone would however bring some downsides. For example code like this would become valid:
> \`\`\`zig
> test {
> var y: u32 = 0; 
> var x = (y = 1); // declares variable x with type/value void, assigns 1 to y
> }
> \`\`\`
> This is obviously undesirable, therefore I propose disallowing expressions of type void in all contexts except:
> 1. As a statement in a block.
> 2. As the operand of \`defer\`/\`errdefer\`/\`suspend\`.
> 4. In the continue expression of a while loop.
> 5. In the body and else branch of \`if\`/\`while\`/\`for\` if the expression as a whole evaluates to type void.
> 6. In \`switch\` cases if the switch as a whole evaluates to type void.
> 7. As the operand of \`comptime\`/\`nosuspend\` if the expression as a whole evaluates to type void.
> 
> Note that rules 4, 5, and 6 are transitive, the outer \`if\`/\`while\`/\`for\`/\`switch\`/\`comptime\`/\`nosuspend\` must also appear in a context where void expressions are permitted.
> 
> I also propose that an exception be made for the empty block \`{}\` which is permitted as an expression in all contexts as the canonical void value.
> 
> These proposed rules would make the \`var x = (y = 1);\` snippet a compile error while allowing the motivating examples above.
> 
> These rules would also effectively implement #9059. As discussed there, there are already cases in the language where allowing void expressions causes confusion and this proposal would help eliminate those.
> \</details\>

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 28, 2026, 5:47am UTC](https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120/4 "2026-10-28T05:47:29Z")

</div>

> [@castholm](#):
>
> `WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN`

and in the doc:

> AssignExpr ← Expr (AssignOp Expr / (COMMA Expr)+ EQUAL Expr / !AssignOp !COMMA)  
> …  
> Expr ← PrefixExpr (OrElseExpr / CatchExpr / BoolExpr)  
> …  
> PrefixExpr ← PrefixOp\* !PrefixOp PrimaryExpr

I don’t understand these well. Does it mean continue-expressions must be AssignExpr? It looks functions calls can also be used as continue-expressions now. Are function calls AssignExpr?

---

<div class="post-metadata">

**Author:** ![Solarflurry](https://ziggit.dev/user_avatar/ziggit.dev/solarflurry/32/10229_2.png) [@Solarflurry](https://ziggit.dev/u/Solarflurry)\
**Post date:** [October 28, 2026, 9:44am UTC](https://ziggit.dev/t/the-zig-doc-treats-i-1-as-an-expression-but-is-it/18120/5 "2026-10-28T09:44:43Z")

</div>

TL;DR: `AssignExpr` doesn’t necessarily mean the expression actaully has any assignment operators, might just be an integer literal or function call.

I’m pretty sure Zig makes the distinction of `Expr` and `AssignExpr` because of types in variable declarations. For example, if you have

```zig
const i: usize = 0;

```

Then, the parser would go through functions something like this:  
`parseContainerMember` (the var decl)  
`...`  
`parseExpr` (for the `usize`)

Because the form `a = b` is considered an ‘expression’ in Zig, then the parser could consume `usize = 0` as the type expression, instead of just `usize`. The AssignExpr is just there so this sort of ambiguity doesn’t occur.

Function calls _are_ AssignExpr because function calls lie under the PrimaryExpr term. It is confusing, because just because a term is _called_ TypeExpr doesn’t mean it _actually is_ a type, same with AssignExpr. Zig splits it into something like this, with the higher functions calling the lower functions:

`parseAssignExpr` → assignment operators  
`parseExpr` → infix operators and precedence  
`parsePrefixExpr` → prefix operators  
`parsePrimaryExpr` → blocks, ifs, switches, whiles, control flow  
`parseTypeExpr` → error `!` and `||` operators  
`parsePrimaryTypeExpr` → identifiers, literals

As you can see, the identifiers or variables might not be types, but they are still contained in the `parsePrimaryTypeExpr` because they _might_ be types, and more general expr parsing functions could call them.

Most of the time, Zig uses `parseExpr` for general expressions in say functions and stuff, uses `parseTypeExpr` where a type is expected (like `fn ident () T`) and `parseAssignExpr`, the most general expr parsing function, I think _only_ when parsing while continue expressions and expressions blocks.
