In C, C++ and C# when using a condition inside a function or loop statement it\'s possible to use a continue or return statement as early as possible and g
1) Input or object state validation. Following code:
void function() {
if( condition ) {
//do some stuff
return;
}
//do other stuff
}
is good when the condition is some requirement for function to work. It's a stage of input validation or object state validation. It then feels right to use return immediately to emphasis, that function did not run at all.
2) Multistage processing. While/continue is good when the loop pops elements from some collection and processes them in multistage manner:
while(foo = bar.getNext()) {
if(foo.empty())
continue;
if(foo.alreadyProcessed())
continue;
// Can we take a shortcut?
if(foo.tryProcessThingsYourself())
continue;
int baz = foo.getBaz();
if(baz < 0) {
int qux = foo.getQux();
if(qux < 0) {
// Error - go to next element
continue;
}
}
// Finally -- do the actual processing
baz = baz * 2;
foo.setBaz(baz);
}
The example shows how natural it is to use continue in scenario when series of multistage processing is done, when each processing can be interrupted by various conditions in various places.
Notice: plinth posted real-life example, which follows what 2) says.
3) General rule. I use continue and return when it corresponds with fact that something has been interrupted. I use else, when the else is part of actual processing.