3 ms·
I think "break" and "continue" have their place. The code he used is a bit of a straw man IMO: while (someCondition) { if (someOtherCondition) { d
by chrisbennet 12y ago
I think "break" and "continue" have their place.
The code he used is a bit of a straw man IMO:
while (someCondition) {
if (someOtherCondition) {
doSomeLogic();
continue; //jump to the top of the loop
}
doSomeOtherLogic();
break; //exit the loop
}
Compare this to how I use "break" and "continue"
for (int j = 0; j < nSomethings; j++)
{
int nDroidsLeftInQ = GetNDroidsInQueue();
if (nDroidsLeftInQ == 0)
break; // blow this clambake, we're done
Droid droid = GetDroidFromQueue();
// Skip droids that aren't even close
bool theseArentTheDroidsWereLookingFor = (DroidFitnessTest(droid) == -1);
if (theseArentTheDroidsWereLookingFor )
continue;
// 30 more lines code where the work actually gets done
doSomeLogic1();
doSomeLogic2();
...
}