www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - continue in labeled switch

reply monkyyy <crazymonkyyy gmail.com> writes:
from the 2026 update for zig he showed off labeled switches
https://youtu.be/x3hOiOcbgeA?si=rr69iKJOZ2CyKysi

```d
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
Jul 04
parent reply Nick Treleaven <nick geany.org> writes:
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

 ```d
 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.
Jul 05
next sibling parent Nick Treleaven <nick geany.org> writes:
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
Jul 05
prev sibling next sibling parent user1234 <user1234 12.de> writes:
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.
Jul 05
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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

 ```d
 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.
```d 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
Jul 05