Thread overview
continue in labeled switch
Jul 05
monkyyy
Jul 05
user1234
Jul 05
monkyyy
July 05

from the 2026 update for zig he showed off labeled switches
https://youtu.be/x3hOiOcbgeA?si=rr69iKJOZ2CyKysi

foo: switch(0){
  case 0:
    "hi".writeln;
    continue foo(2);
  case 1:
    "bye".writeln;
    break;
  case 2:
    "hello".writeln;
    continue foo(1);
  default: assert(0);
}

would print "hi","hello","bye" this is probably the majority of my uses of goto

someone tell walter zig is better c because of this

July 05

On Saturday, 5 July 2025 at 02:27:33 UTC, monkyyy wrote:

>

from the 2026 update for zig he showed off labeled switches
https://youtu.be/x3hOiOcbgeA?si=rr69iKJOZ2CyKysi

foo: switch(0){
  case 0:
    "hi".writeln;
    continue foo(2);
  case 1:
    "bye".writeln;
    break;
  case 2:
    "hello".writeln;
    continue foo(1);
  default: assert(0);
}

would print "hi","hello","bye" this is probably the majority of my uses of goto

Why is that better than using goto case 1;, goto case 2;?

https://dlang.org/spec/statement.html#goto-statement

Using continue for this seems like unnecessary keyword meaning overloading, given that AIUI continue; still refers to loop statement iteration, and a labelled switch lowers to a while loop(!):

>

Semantically, this is equivalent to the following loop:

https://ziglang.org/documentation/master/#toc-Switching-with-Enum-Literals

So it seems using continue; would silently restart the labelled switch statement, probably causing an infinite loop (if the implementation matches the spec). Or probably that is/will be a compile error, in which case labelled switch has limitations versus unlabelled switch.

Also is there a continue foo(default);? Having goto default; is better than having to write e.g. continue foo(3);, because case 3: could be added later, and jumping to the default case may have been the intended target - updating the latter code can easily be forgotten, especially for a long switch statement.

July 05

On Saturday, 5 July 2025 at 10:18:28 UTC, Nick Treleaven wrote:

>

Using continue for this seems like unnecessary keyword meaning overloading, given that AIUI continue; still refers to loop statement iteration, and a labelled switch lowers to a while loop(!):

>

Semantically, this is equivalent to the following loop:

https://ziglang.org/documentation/master/#toc-Switching-with-Enum-Literals

Sorry, wrong link:
https://ziglang.org/documentation/master/#Labeled-switch

July 05

On Saturday, 5 July 2025 at 10:18:28 UTC, Nick Treleaven wrote:

>

Why is that better than using goto case 1;, goto case 2;?

The example of OP is not good, the point is that you can jump from a nested switch to its parent switch.

July 05

On Saturday, 5 July 2025 at 10:18:28 UTC, Nick Treleaven wrote:

>

On Saturday, 5 July 2025 at 02:27:33 UTC, monkyyy wrote:

>

from the 2026 update for zig he showed off labeled switches
https://youtu.be/x3hOiOcbgeA?si=rr69iKJOZ2CyKysi

foo: switch(0){
  case 0:
    "hi".writeln;
    continue foo(2);
  case 1:
    "bye".writeln;
    break;
  case 2:
    "hello".writeln;
    continue foo(1);
  default: assert(0);
}

would print "hi","hello","bye" this is probably the majority of my uses of goto

Why is that better than using goto case 1;, goto case 2;?

https://dlang.org/spec/statement.html#goto-statement

Using continue for this seems like unnecessary keyword meaning overloading, given that AIUI continue; still refers to loop statement iteration, and a labelled switch lowers to a while loop(!):

>

Semantically, this is equivalent to the following loop:

https://ziglang.org/documentation/master/#toc-Switching-with-Enum-Literals

So it seems using continue; would silently restart the labelled switch statement, probably causing an infinite loop (if the implementation matches the spec). Or probably that is/will be a compile error, in which case labelled switch has limitations versus unlabelled switch.

Also is there a continue foo(default);? Having goto default; is better than having to write e.g. continue foo(3);, because case 3: could be added later, and jumping to the default case may have been the intended target - updating the latter code can easily be forgotten, especially for a long switch statement.

void collez(int i){
	final switch(i%2){
		case 0:
			i/=2;
			goto case (i%2); //error
		case 1:
			i*=3;i++;
			goto case (i%2);
}}

reusing the switch machinery