Goto is rarely, if ever the correct solution. Even oogabooga's code doesn't warrant goto:
Code:
void its_own_function() {
while (whatever) {
while (something) {
if (such_and_such)
return;
actual_work();
}
}
}
or
Code:
while (whatever && something && !such_and_such) {
actual_work();
}
are acceptable and non-goto solutions. In the case of bigger loops that cannot be simply combined like this, they should usually be split into separate functions.
----
For example, if I had a function to go through an array of IPs, connect to them, and send 5 packets each:
Code:
for (i = 0; i < length; i++) {
if (!connect())
goto out;
for (j = 0; j < 5; j++)
if (!send())
goto out;
}
out:
you could split it:
Code:
int send5(){
for (j = 0; j < 5; j++)
if (!send())
return -1;
}
int connecttoips() {
for (i = 0; i < length; i++) {
if (!connect())
break;
if (send5() < 0)
break;
}
}
although in this case, it really isn't necessary.