You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: zig/doc/langref.html.in
+47Lines changed: 47 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2495,6 +2495,53 @@ or
2495
2495
</p>
2496
2496
{#code|test_exhaustive_switch.zig#}
2497
2497
2498
+
{#header_close#}
2499
+
2500
+
{#header_open|Labeled switch#}
2501
+
<p>
2502
+
When a switch statement is labeled, it can be referenced from a
2503
+
{#syntax#}break{#endsyntax#} or {#syntax#}continue{#endsyntax#}.
2504
+
{#syntax#}break{#endsyntax#} will return a value from the {#syntax#}
2505
+
switch{#endsyntax#}.
2506
+
</p>
2507
+
<p>
2508
+
A {#syntax#}continue{#endsyntax#} targeting a switch must have an
2509
+
operand. When executed, it will jump to the matching prong, as if the
2510
+
{#syntax#}switch{#endsyntax#} were executed again with the {#syntax#}
2511
+
continue{#endsyntax#}'s operand replacing the initial switch value.
2512
+
</p>
2513
+
2514
+
{#code|test_switch_continue.zig#}
2515
+
2516
+
<p>
2517
+
Semantically, this is equivalent to the following loop:
2518
+
</p>
2519
+
{#code|test_switch_continue_equivalent.zig#}
2520
+
2521
+
<p>
2522
+
This can improve clarity of (for example) state machines, where the syntax {#syntax#}continue :sw .next_state{#endsyntax#} is unambiguous, explicit, and immediately understandable.
2523
+
</p>
2524
+
<p>
2525
+
However, the motivating example is a switch on each element of an array, where using a single switch can improve clarity and performance:
2526
+
</p>
2527
+
{#code|test_switch_dispatch_loop.zig#}
2528
+
2529
+
<p>
2530
+
If the operand to {#syntax#}continue{#endsyntax#} is
2531
+
{#link|comptime#}-known, then it can be lowered to an unconditional branch
2532
+
to the relevant case. Such a branch is perfectly predicted, and hence
2533
+
typically very fast to execute.
2534
+
</p>
2535
+
2536
+
<p>
2537
+
If the operand is runtime-known, each {#syntax#}continue{#endsyntax#} can
2538
+
embed a conditional branch inline (ideally through a jump table), which
2539
+
allows a CPU to predict its target independently of any other prong. A
2540
+
loop-based lowering would force every branch through the same dispatch
0 commit comments