C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] Labeled 'continue' and 'break'
From: jianping z (zjpwork_at_[hidden])
Date: 2020-03-11 21:19:08


I think it would look much better with label being added inside "for(label: ...) {}" for continue/break

for ( label1: int x = 0; x < 5; x++)
{
  for ( label2: int y = 0; y < 5; y++)
  {
    for (int z = 0; z < 5; z++)
    {
      std::cout << x << ',' << y << ',' << z << std::endl;
      if (z+y == 8) continue label2;
      if (z+x == 3) break label2;
      if (x + y + z == 13) break label1;
    }
  }
}

this type of label can be called "block label", and it would work for "while(label: ...) {}", "do {} while(label: ...)".

it could also work for "switch(label:...) {}", it is useful to break out of switch from nested loops inside its case statement.

int choice;
...
switch(label_switch: choice)
{
case 1:
  ...
  do_something_before();
  for ( label1: int x = 0; x < 5; x++)
  {
    for ( label2: int y = 0; y < 5; y++)
    {
      for (int z = 0; z < 5; z++)
      {
        std::cout << x << ',' << y << ',' << z << std::endl;
        if (z+y == 8) continue label2;
        if (z+x == 3) break label2;
        if (x + y + z == 13) break label1;
        if (x + y + z == 18) break label_switch; //easy to break out of switch
      }
    }
  }
  do_something_after();
  ...
  break;
case 2:
....
}

how do you think?

Thanks

On 3/10/2020 06:35 PM, Jake Arkinstall via Std-Proposals wrote:
I think the break label syntax is a little bit confusing to the casual reader, given that it's actually going to jump past the loop described by the label. Continue too, to some extent, because it jumps to the evaluation of the condition, whereas the immediate code after the label is the initialisation.

Of course, this confusion is just caused by expecting goto like behaviour, when the implementation would actually be a goto to a label based on the loop label*. I have actually seen goto used for this circumstance in relatively modern C++ code, particularly when the "return from a lambda" approach doesn't suffice (I.e. when you want to break from a number of loops that depend on a condition).

I'd actually make the labels optional and have break N, where N is how many loops to break out of. Same goes for continue. I think this is neater, but that's probably some (rare) nostalgia from my PHP days.

* such that

label1: for(int i = 0; x < 5; x++){
    if(x % 3 == 1){
        continue label1;
    }else if(x % 3 == 2){
        break label1;
    }
}

would be equivalent to


{
    int x = 0;
    while(x < 5){
        if(x % 3 == 1){
            goto label1_continue;
        }else if(x % 3 == 2){
            goto label1_break;
        }
label1_continue:
        x++;
   }
label1_break:


On Wed, 11 Mar 2020, 00:12 Ryan Nicholl via Std-Proposals, <std-proposals@lists.isocpp.org> wrote:

I'd like to suggest adopting labeled break and continue statements,

e.g.

label1: for( int x = 0; x < 5; x++)
{
label2: for (int y = 0; y < 5; y++)
{
for (int z = 0; z < 5; z++)
{
std::cout << x << ',' << y << ',' << z << std::endl;
if (z+y == 8) continue label2;
if (z+x == 3) break label2;
if (x + y + z == 13) break label1;
}
}
}

Other languages have it, and it's convenient with nested loops.

--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals


STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups