Topic: Breaking out of nested loops


Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Mon, 3 Jul 2006 10:45:46 CST
Raw View
In article <nevin-89115A.00254703072006@news.isp.giganews.com>,
 nevin@eviloverlord.com ("Nevin \":-]\" Liber") wrote:

> ===================================== MODERATOR'S COMMENT:
>  Please take care to keep responses topical for csc++.
>
>
> ===================================== END OF MODERATOR'S COMMENT
> In article
> <howard.hinnant-7B4330.15285502072006@syrcnyrdrs-02-ge0.nyroc.rr.com>,
>  howard.hinnant@gmail.com (Howard Hinnant) wrote:
>
> > Personally I find the goto more readable.  goto isn't useful very often.
> > But when it is useful, it can be the best choice.  Coding guidelines
> > that recommend that it (unconditionally) never be used are (imho)
> > shortsighted.
>
> While I'm not a big fan of coding guidelines that deal in absolutes, do
> you have a concrete example where goto is clearly better than a more
> structured approach?

Sure.  The OP's original code is such an example.  Imho Steve summed up
the rationale very well:

In article <1151866267.385851@news1nwk>,
 stephen.clamage@sun.com (Steve Clamage) wrote:

> Your toy example looks OK when you write it, but what happens when this
> part of the code changes? If you change the loop nesting, you then have
> to examine every break statement, match up the curly braces, and see if
> the break count also needs to change.
>
> If a later programmer (who might be you) forgets or counts wrong, you
> might be unlucky enough to have only a subtle change in program behavior
> that is difficult to find.

-Howard

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Sun, 2 Jul 2006 17:53:40 GMT
Raw View
I haven't thought this through or anything, but it seemed kinda neat when
I thought of it...

How about extending the functionality of the "break" statement to
indicate how many breaks you want to perform? For instance, instead of
having:

    while (expr1)
    {
        while(expr2)
        {
            while(expr3)
            {
                goto OUT_OF_LOOP2;
            }
        }

        OUT_OF_LOOP2: ;
    }



We could have:


    while (expr1)
    {
        while(expr2)
        {
            while(expr3)
            {
                break(2);    /* Break out twice */
            }
        }
    }



--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: howard.hinnant@gmail.com (Howard Hinnant)
Date: Sun, 2 Jul 2006 20:53:25 GMT
Raw View
In article <Y7Spg.10958$j7.315197@news.indigo.ie>,
 fgothamNO@SPAM.com (Frederick Gotham) wrote:

> I haven't thought this through or anything, but it seemed kinda neat when
> I thought of it...
>
> How about extending the functionality of the "break" statement to
> indicate how many breaks you want to perform? For instance, instead of
> having:
>
>     while (expr1)
>     {
>         while(expr2)
>         {
>             while(expr3)
>             {
>                 goto OUT_OF_LOOP2;
>             }
>         }
>
>         OUT_OF_LOOP2: ;
>     }
>
>
>
> We could have:
>
>
>     while (expr1)
>     {
>         while(expr2)
>         {
>             while(expr3)
>             {
>                 break(2);    /* Break out twice */
>             }
>         }
>     }

Personally I find the goto more readable.  goto isn't useful very often.
But when it is useful, it can be the best choice.  Coding guidelines
that recommend that it (unconditionally) never be used are (imho)
shortsighted.

-Howard

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: Sun, 2 Jul 2006 23:43:19 GMT
Raw View
Your suggestion appears a few times per year.

Your toy example looks OK when you write it, but what happens when this
part of the code changes? If you change the loop nesting, you then have
to examine every break statement, match up the curly braces, and see if
the break count also needs to change.

If a later programmer (who might be you) forgets or counts wrong, you
might be unlucky enough to have only a subtle change in program behavior
that is difficult to find.

---
Steve Clamage, stephen.clamage@sun.com

Frederick Gotham wrote:

> I haven't thought this through or anything, but it seemed kinda neat when
> I thought of it...
>
> How about extending the functionality of the "break" statement to
> indicate how many breaks you want to perform? ...
>
>     while (expr1)
>     {
>         while(expr2)
>         {
>             while(expr3)
>             {
>                 break(2);    /* Break out twice */
>             }
>         }
>     }
>
>
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: mvglen04-cnews@yahoo.com (dayton)
Date: Sun, 2 Jul 2006 23:48:52 GMT
Raw View
Other computer languages allow you to name loops, then use the
name in the break statement.

OUTERMOSTLOOP:
while (expr1)   {
   while(expr2) {
     while (expr3) {
       break OUTMOSTLOOP;
     }
   }
}

/Glen


Howard Hinnant wrote:
>>> How about extending the functionality of the "break" statement to
>> indicate how many breaks you want to perform? For instance, instead of
>> having:
>>
>>     while (expr1)
>>     {
>>         while(expr2)
>>         {
>>             while(expr3)
>>             {
>>                 goto OUT_OF_LOOP2;
>>             }
>>         }
>>
>>         OUT_OF_LOOP2: ;
>>     }
>>
>>
>>
>> We could have:
>>
>>
>>     while (expr1)
>>     {
>>         while(expr2)
>>         {
>>             while(expr3)
>>             {
>>                 break(2);    /* Break out twice */
>>             }
>>         }
>>     }

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: nagle@animats.com (John Nagle)
Date: Sun, 2 Jul 2006 23:49:21 GMT
Raw View
Frederick Gotham wrote:
> I haven't thought this through or anything, but it seemed kinda neat when
> I thought of it...
>
> How about extending the functionality of the "break" statement to
> indicate how many breaks you want to perform? For instance, instead of
> having:

     If you need to get out of multiple loops, "return" is usually
the cleanest way.  This forces you to encapsulate the complex
control structure in a function.

     Of course, the fact that C++ lacks local functions, and
requires the declaration of private function members in
the class declaration, does make this more difficult.

    John Nagle

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: nevin@eviloverlord.com ("Nevin \":-]\" Liber")
Date: Mon, 3 Jul 2006 09:01:49 GMT
Raw View
===================================== MODERATOR'S COMMENT:
 Please take care to keep responses topical for csc++.


===================================== END OF MODERATOR'S COMMENT
In article
<howard.hinnant-7B4330.15285502072006@syrcnyrdrs-02-ge0.nyroc.rr.com>,
 howard.hinnant@gmail.com (Howard Hinnant) wrote:

> Personally I find the goto more readable.  goto isn't useful very often.
> But when it is useful, it can be the best choice.  Coding guidelines
> that recommend that it (unconditionally) never be used are (imho)
> shortsighted.

While I'm not a big fan of coding guidelines that deal in absolutes, do
you have a concrete example where goto is clearly better than a more
structured approach?

--
 Nevin ":-)" Liber  <mailto:nevin@eviloverlord.com>  (773) 961-1620

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: cbarron3@ix.netcom.com (Carl Barron)
Date: Mon, 3 Jul 2006 15:21:20 GMT
Raw View
Howard Hinnant <howard.hinnant@gmail.com> wrote:

> In article <Y7Spg.10958$j7.315197@news.indigo.ie>,
>  fgothamNO@SPAM.com (Frederick Gotham) wrote:
>
> > I haven't thought this through or anything, but it seemed kinda neat when
> > I thought of it...
> >
> > How about extending the functionality of the "break" statement to
> > indicate how many breaks you want to perform? For instance, instead of
> > having:
> >
> >     while (expr1)
> >     {
> >         while(expr2)
> >         {
> >             while(expr3)
> >             {
> >                 goto OUT_OF_LOOP2;
> >             }
> >         }
> >
> >         OUT_OF_LOOP2: ;
> >     }
> >
> >
> >
> > We could have:
> >
> >
> >     while (expr1)
> >     {
> >         while(expr2)
> >         {
> >             while(expr3)
> >             {
> >                 break(2);    /* Break out twice */
> >             }
> >         }
> >     }
>
> Personally I find the goto more readable.  goto isn't useful very often.
> But when it is useful, it can be the best choice.  Coding guidelines
> that recommend that it (unconditionally) never be used are (imho)
> shortsighted.
>
  I prefer  OUT_OF_LOOP2:;}  placing the label and the } on the
same line makes inserting code incorrectly between the label and the }
ending the loop more difficult.

If gotos must be avoided at 'all costs' then a local class can be used
to simulate a 'local function' whose body contains the code for the
inner two loops replacing 'break(2)' with return.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]