continue

B

Bern

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

Risto Lankinen

Bern said:
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 -
 
R

Rolf Magnus

Bern said:
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.
 
K

Kai-Uwe Bux

Bern said:
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
 
J

Jacek Dziedzic

Bern said:
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.
 
D

Derrick Coetzee

Bern said:
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.
 
M

Mabden

Bern said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top