continue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bern

    continue


    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.... .


  • Risto Lankinen

    #2
    Re: continue


    " Bern" <x@x.com> wrote in message news:41382d26@n ews.starhub.net .sg...[color=blue]
    >
    > 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[/color]
    2[color=blue]
    > loops?[/color]

    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 -


    Comment

    • Rolf Magnus

      #3
      Re: continue

      Bern wrote:
      [color=blue]
      > 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?[/color]

      What's the reason for not using goto for this?
      [color=blue]
      > 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?[/color]

      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.
      [color=blue]
      > I basically find it amusing--we are discouraged to use goto as a practice
      > of good progrtamming,[/color]

      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.
      [color=blue]
      > but C++ does not provide support for such things in programming.... .[/color]

      It provides goto.

      Comment

      • Kai-Uwe Bux

        #4
        Re: continue

        Bern wrote:
        [color=blue]
        >
        > 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?[/color]

        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.

        [color=blue]
        > 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.... .[/color]

        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

        Comment

        • Jacek Dziedzic

          #5
          Re: continue

          Bern wrote:
          [color=blue]
          > 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?[/color]

          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.

          Comment

          • Derrick Coetzee

            #6
            Re: continue

            Bern wrote:[color=blue]
            > Is there any other solution besides resorting to using goto?[/color]

            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(o uter_loop);
            if(whatever2) break_loop(oute r_loop);
            }
            outer_loop_cont inue:
            }
            outer_loop_brea k:

            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.

            Comment

            • Mabden

              #7
              Re: continue

              " Bern" <x@x.com> wrote in message news:41382d26@n ews.starhub.net .sg...[color=blue]
              >
              > 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?[/color]

              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.....
              }
              -------------------------------------------
              [color=blue]
              > Or is there a way to rearrange the whole structure to eliminate the[/color]
              use of 2[color=blue]
              > loops?[/color]

              Only you can decide that.


              Comment

              Working...