I\'m doing a project with basic Java for a CS class. The project has a for loop nested inside a while loop.
I am not allowed to use break
as a wa
break
and return
are not the same. break
will halt the execution of the inner-most loop, return
will return from the current method - ie nothing more in the method will be executed. As far as I know there's nothing wrong with using break in a loop; presumably this is an artificial requirement in order to get you to think of alternative logic. And there's nothing wrong with using a return
either; this will save you from horrible nested conditionals (if
clauses).
If you are not allowed to use break
, then it makes sense not to use return
as well, since every loop with break
can be rewritten as a loop with return
(by outsourcing it into a separate function).
From a non-homework point of view, break
(and return
) are very useful language features which are usually not considered bad style (of course, every language construct can be abused to write bad code).
In your homework scenario, however, I guess it would defeat the point of the homework to just "workaround" the missing break
by using return
. You should think of an alternative way to solve your task.
I tried to put a boolean operator in the while loop that controls the for loop but that doesn't control what goes on inside the for loop until the for loop gets to the end.
That's true, but, by using the if
statement, you can also execute/not execute code inside the loop depending on your boolean.
This is about perfect code.
for(int i; i < stop; i++) {
boolean flag = true;
while(flag && condition) {
//some action
if(shouldIstop) {
flag = false;
}
}
}
When you set the flag to false then you will exit from the while, when you will set i to stop then you also will exit from for.
From a purist viewpoint, break arguably shouldn't be used. The point of a while loop is that you use the condition at the start of the loop to break out of it, and things can potentially get confusing if you start to flood your loop with break statements as oppose to getting the condition correct (which is a quick hack students may well be tempted to use if they can't work out how to do things the proper way!) The same goes for continue and returning in the middle of loops.
All these language features are there for a reason and yes, sometimes they're the best way of doing things. When just starting off however, the chances of you using them judiciously and properly are rare, they're more likely to be used as free "hack to get me out of jail without understanding how this thing works" cards. And that, I'd say, is a good enough reason to ban their use in basic programming courses.
This question seems to enter the territory of a very old still unresolved argument, the "Single vs. Multiple method exit points".
So if using return in a loop is worrying you (or your teacher), you (or he) could have a look here.
To answer your question. In my (and not only my) opinion using a return in a loop is definitely OK.
Do note however that overusing it might decrease code readability (see linked answer for details/examples).