C++ Logo

std-proposals

Advanced search

Re: Labeled 'continue' and 'break'

From: Ryan Nicholl <rnicholl_at_[hidden]>
Date: Wed, 11 Mar 2020 03:51:46 +0000
Not really allowed to share (closed source codebase) but it's enough to say that, when you have multiple nested loops, having the feature is convenient. I could try to make a more "realistic" example, but that might still be contrived. However, note a few things: goto can't cross declaration boundaries, which would require for (...) {{ } label_continue:;} syntax, which I find odd. Second, I think goto is less readable. Third, many companies ban use of "goto" keyword, and I think this offers a cleaner alternative that doesn't require the forbidden fruit.

--
Ryan Nicholl
(678)-358-7765
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, March 10, 2020 11:26 PM, Jorg Brown <jorg.brown_at_[hidden]> wrote:
> I think you'd make a much more compelling case if this were a real scenario, rather than merely a pseudo-random integer loop with names like "x", "y", "z", "label1" and "label2".  As it stands, the labels don't provide any added value to this code.
>
> With current C++, I'd have to write it this way:
>
> int main() {
>     for (int x = 0; x < 5; x++) {
>         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) goto continue_y_loop;
>                 if (z + x == 3) goto break_y_loop;
>                 if (x + y + z == 13) goto break_x_loop;
>             }
>             continue_y_loop:;
>         }
>         break_y_loop:;
>     }
>     break_x_loop:;
> }
>
> To be honest, that doesn't seem so bad.
>
> = - = - = - =
>
> May I ask: what scenario made you want to ask for this feature?  Perhaps if you show what you were really trying to do, it could be more compelling?
>
> -- Jorg
>
> On Tue, Mar 10, 2020 at 5:12 PM 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-10 22:54:39