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