C++ Logo

std-proposals

Advanced search

Re: Labeled 'continue' and 'break'

From: Jorg Brown <jorg.brown_at_[hidden]>
Date: Tue, 10 Mar 2020 21:08:54 -0700
Goto can cross declaration boundaries; it just can't be used to goto *into*
a declaration boundary. In any case, for this example, that's a
non-issue. The code I posted does compile and run.
https://godbolt.org/z/o8A_w9

Note that for "break", that's a non-issue, since the break label is always
right after the for loop.

I admit that for "continue", it's much more common to have variables being
defined, that would give an error when you try to goto the end of their
enclosing scope.

If only labels were definable inside for loops' iteration expressions, that
wouldn't be an error. e.g. `for (int y = 0; y < 5; continue_y_loop: y++) {`

-- Jorg

On Tue, Mar 10, 2020 at 8:51 PM Ryan Nicholl <rnicholl_at_[hidden]>
wrote:

> 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 23:11:52