Just one quick question, say, Car class extends HashMap(String, String).
1.
for (Car car : carList) {
if (car.isEmpty) {
break;
}
doSomething();
}
2.
for (Car car : carList) {
if (!car.isEmpty) {
doSomethingElse();
}
}
Which of the above two is better?
Thanks.
—- edited —-
Sorry I did not make my point clear.
The doSomething() method is actually doing different things.
I’ve changed them to doSomething() and doSometingElse().
My question is, will you put all the process in one if()? or first break the loop if the if() condition does not satisfy.
Thanks.
If you actually meant “continue” and not “break”, then I’d say not using continue or break is better.
It’s not a huge deal, but statements like continue, break and return are all essentially goto statements that have been dressed up a little. Instead of goto a line or goto a label they goto the top or bottom of a control structure–it makes the programmer have to think a little bit when they track it which means wasted time and additional chances for errors.
It’s not a terribly big deal, and many times these structures will actually clarify your code, but if you have a choice and they seem to do essentially the same thing, go the way that doesn’t involve sending control elsewhere.