continue

Discussion in 'C++' started by Bern, Sep 3, 2004.

  1. Bern

    Bern Guest

    lets say i have the code:

    while (some condition){ /* the "big" loop */

    .... some code....

    while (some condition){

    ...code...
    if (!some condition){

    ///// How to continue the "big" loop?
    }

    ..some code....
    }

    ...some code.....
    }

    Is there any other solution besides resorting to using goto?
    Or is there a way to rearrange the whole structure to eliminate the use of 2
    loops?

    this situation in Java is not a problem as Java supports labeled breaks and
    continue,
    but why C/C++ doesn't support them?

    I basically find it amusing--we are discouraged to use goto as a practice
    of good progrtamming, but C++ does not provide support for such things in
    programming.....
    Bern, Sep 3, 2004
    #1
    1. Advertising

  2. " Bern" <> wrote in message news:...
    >
    > lets say i have the code:
    >
    > while (some condition){ /* the "big" loop */
    >
    > .... some code....
    >
    > while (some condition){
    >
    > ...code...
    > if (!some condition){
    >
    > ///// How to continue the "big" loop?
    > }
    >
    > ..some code....
    > }
    >
    > ...some code.....
    > }
    >
    > Is there any other solution besides resorting to using goto?
    > Or is there a way to rearrange the whole structure to eliminate the use of

    2
    > loops?


    Here's one way:

    Implement the inner loop in a separate function, and call
    that function from the "big" loop. When the inner loop
    detects the "continue" situation, it simply returns, so that
    the execution continues at that point in the "big" loop:

    void InnerLoop()
    {
    while (some condition){
    ...code...
    if (!some condition){
    return; ///// to continue the "big" loop!
    }
    ..some code....
    }
    }


    while (some condition){ /* the "big" loop */
    .... some code....
    InnerLoop();
    ...some code.....
    }


    Of course, parameters may have to be passed and the meaning
    of a return value defined (if appropriate) to/from the InnerLoop() .

    Also, if the "if (!some condition)" is used to detect an 'exceptional'
    situation, then try/catch/throw can also be used:

    typedef int up;

    while (some condition){ /* the "big" loop */
    .... some code....
    try {
    while (some condition){
    ...code...
    if (!some condition){
    throw up(); // :)
    }
    ..some code....
    }
    }
    catch( up & )
    {
    // deliberately empty
    }
    ...some code.....
    }


    Note, some developers will object this - for a reason, really - if
    the condition to throw is not 'exceptional' enough. Only you can
    judge what is exceptional enough, though... :)

    Cheers!

    - Risto -
    Risto Lankinen, Sep 3, 2004
    #2
    1. Advertising

  3. Bern

    Rolf Magnus Guest

    Bern wrote:

    > lets say i have the code:
    >
    > while (some condition){ /* the "big" loop */
    >
    > .... some code....
    >
    > while (some condition){
    >
    > ...code...
    > if (!some condition){
    >
    > ///// How to continue the "big" loop?
    > }
    >
    > ..some code....
    > }
    >
    > ...some code.....
    > }
    >
    > Is there any other solution besides resorting to using goto?


    What's the reason for not using goto for this?

    > Or is there a way to rearrange the whole structure to eliminate the use of
    > 2 loops?
    >
    > this situation in Java is not a problem as Java supports labeled breaks
    > and continue, but why C/C++ doesn't support them?


    C++ has goto, which can do the same. It also jumps to a label. The
    difference is just that it doesn't need to be a jump out of a loop.

    > I basically find it amusing--we are discouraged to use goto as a practice
    > of good progrtamming,


    Says who? You're discouraged to _abuse_ goto, just like you are discouraged
    to abuse any other language feature. I acually find it funny that so many
    people seem to be told that goto is evil and simply believe that they must
    never use it even if that usage would be perfectly valid just because
    "though shalt never use goto" or something.

    > but C++ does not provide support for such things in programming.....


    It provides goto.
    Rolf Magnus, Sep 3, 2004
    #3
  4. Bern

    Kai-Uwe Bux Guest

    Bern wrote:

    >
    > lets say i have the code:
    >
    > while (some condition){ /* the "big" loop */
    >
    > .... some code....
    >
    > while (some condition){
    >
    > ...code...
    > if (!some condition){
    >
    > ///// How to continue the "big" loop?
    > }
    >
    > ..some code....
    > }
    >
    > ...some code.....
    > }
    >
    > Is there any other solution besides resorting to using goto?
    > Or is there a way to rearrange the whole structure to eliminate the use of
    > 2 loops?
    >
    > this situation in Java is not a problem as Java supports labeled breaks
    > and continue,
    > but why C/C++ doesn't support them?


    I do not really see that goto is worse programming practice than attaching
    labels to break and continue. Every once in a while, goto is just the right
    thing to use. In fact, this seems to be a comparatively innocent case.

    However, very often running into the need of goto or break labels indicates
    that the algorithm might not be well planned. You did not provide enough
    code for us the discuss this, so I will assume that you really need to exit
    from an inner loop by two levels.


    > I basically find it amusing--we are discouraged to use goto as a practice
    > of good progrtamming, but C++ does not provide support for such things in
    > programming.....


    Well, the *really* confusing uses of goto are those where you exit a
    routine via goto for a jump to some error handler. This is about language
    support for programming in the large. C++ has exceptions for those global
    jumps, and even makes guarantees about cleanup involved in stack unwinding.
    For local jumps, it appears that goto is just the more powerful way of
    messing up your code.

    I highly recommend:

    D.E. Knuth: Structured Programming with goto Statements
    (Computing Surveys, Vol 6, No 4, Dec 1974, p 261--301)


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 3, 2004
    #4
  5. Bern wrote:

    > lets say i have the code:
    >
    > while (some condition){ /* the "big" loop */
    >
    > .... some code....
    >
    > while (some condition){
    >
    > ...code...
    > if (!some condition){
    >
    > ///// How to continue the "big" loop?
    > }
    >
    > ..some code....
    > }
    >
    > ...some code.....
    > }
    >
    > Is there any other solution besides resorting to using goto?
    > Or is there a way to rearrange the whole structure to eliminate the use of 2
    > loops?


    You missed Dijkstra's point :). Your example is the classic most
    legitimate use of 'goto' statement there is. Therefore don't
    "resort to using goto" here, just "use goto here".

    HTH,
    - J.
    Jacek Dziedzic, Sep 3, 2004
    #5
  6. Bern wrote:
    > Is there any other solution besides resorting to using goto?


    In languages like Java which have labelled breaks and continues, these
    are better. From the C Rationale:

    "3.6.6.2 The continue statement

    The Committee rejected proposed enhancements to continue and break which
    would allow specification of an iteration statement other than the
    immediately enclosing one, on grounds of insufficient prior art."

    It's a good idea that now has "prior art"; it makes the purpose more
    explicit. C just didn't do it. Use goto - it's not a big deal. If you
    want to make it more explicit, try out these macros:

    #define continue_loop(x) goto x##_continue
    #define break_loop(x) goto x##_break

    Then you can do:

    while(...) {
    while(...) {
    if(whatever) continue_loop(outer_loop);
    if(whatever2) break_loop(outer_loop);
    }
    outer_loop_continue:
    }
    outer_loop_break:

    Another common solution is to form a chain of breaks/continues. For example:

    while(...) {
    int i;
    for(i=0; i<n; i++) {
    if (evil is afoot) break;
    ...
    }
    if (i < n) {
    /* We get here if and only if we did a break above */
    break; /* or continue */
    }
    ...
    }

    All in all, a goto seems clearer and less error-prone than this
    approach, especially for longer chains.
    --
    Derrick Coetzee
    I grant this newsgroup posting into the public domain. I disclaim all
    express or implied warranty and all liability. I am not a professional.
    Derrick Coetzee, Sep 4, 2004
    #6
  7. Bern

    Mabden Guest

    " Bern" <> wrote in message news:...
    >
    > lets say i have the code:
    >
    > while (some condition){ /* the "big" loop */
    > .... some code....
    > while (some condition){
    > ...code...
    > if (!some condition){
    > ///// How to continue the "big" loop?
    > }
    > ..some code....
    > }
    > ...some code.....
    > }
    >
    > Is there any other solution besides resorting to using goto?


    The "only good use" of a goto is a forward reference to jump out of
    multiple embedded loops. It's "bad" to use a goto to do looping or to
    jump into other functions.

    Another way to do what you want is by using flags. This gets tedious if
    there are many different ones, and adds an extra check at each loop
    point. Here's how that's done (this better not be homework!):

    -------------------------------------------
    continue_flag = TRUE;
    while (some condition){ /* the "big" loop */
    .... some code....
    while (TRUE == continue_flag && some condition){
    ...code...
    if (!some condition){
    continue_flag = FALSE; /* set continue inner loop flag */
    break; /* avoids running the "some inner code" */
    }
    ..some inner code....
    }
    ...some outer code.....
    }
    -------------------------------------------

    > Or is there a way to rearrange the whole structure to eliminate the

    use of 2
    > loops?


    Only you can decide that.
    Mabden, Sep 7, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam

    ASP to continue processing.

    Sam, Sep 22, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    1,844
    Jonathan Allen
    Sep 25, 2004
  2. Xavier Osa
    Replies:
    0
    Views:
    618
    Xavier Osa
    Jan 9, 2004
  3. dotnetforfood
    Replies:
    18
    Views:
    690
    =?Utf-8?B?QWwtRmVlZA==?=
    Jul 2, 2004
  4. =?Utf-8?B?RGlnZ2xlcg==?=

    Waiting for a file deletion to continue

    =?Utf-8?B?RGlnZ2xlcg==?=, Oct 13, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    359
    =?Utf-8?B?RGlnZ2xlcg==?=
    Oct 14, 2004
  5. Chris
    Replies:
    1
    Views:
    2,497
    Cirrosi
    Oct 16, 2004
Loading...

Share This Page