C++ Logo

std-proposals

Advanced search

Re: Labeled 'continue' and 'break'

From: Ryan Nicholl <rnicholl_at_[hidden]>
Date: Thu, 12 Mar 2020 23:46:42 +0000
Or perhaps better looking,

for label: ( int i = 0; i < 5; i++)
{
...
break label;
}

No confusion with goto this way, although the label: for (...) approach has an additional advantage,

// Loop labels inside the loop:
label1: for ( label2: ...) {...} // Inner Label Proposal
// two labels if "goto label1" is needed as well as "continue label2;"
// Same problem with "for label2: (...) { ... }" syntax, "Outer Label Proposal"

// Loop labels before the loop:
label: for (...) {...}
// "goto label;" and "continue label;" both work, do different things.
// One Label to Rule Them All (The One Label Proposal)

I still prefer the one label variant, but any of these three variations could work.

-------- Original Message --------
On Mar 11, 2020, 22:19, jianping z via Std-Proposals wrote:

> 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_at_[hidden]> 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_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-03-12 18:49:39