Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad style to use 'return' to break a for loop in Java?

Tags:

java

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 way of ending a for loop early. I found out that return seems to have the same effect as break. Is it bad style to use return as a way to break the loop?

My for loop has to check through three different statements but if it finds one that is true then it is supposed to end straight away without continuing to check the rest of the statements.

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.

Also does it matter if return doesn't return anything?

299/01/11 Update: Thanks everyone so much for your comments. I found it really helpful to read through all the debates.

I spoke to my tutor and it turns out for the purposes of getting full marks, I shouldn't use return either.

So I found the advice about setting a boolean in the 'for' loop really helpful, as I didn't know you could do that.

like image 565
Lemony Avatar asked Jan 26 '11 13:01

Lemony


People also ask

Is it bad to return from a for loop?

But whether you break and then return or return early is a matter of taste; at least in this case. This function is easy to read and comprehend, so, more than one return does no harm. In longer functions with multiple returns, it might be hard to get all the exit points. I would go for what is better readable for you.

Does return break out of for loop Java?

Yes, usually (and in your case) it does break out of the loop and returns from the method.

Is it bad practice to break for loops?

Using break as well as continue in a for loop is perfectly fine. It simplifies the code and improves its readability.

Is it OK to use break in Java?

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).


1 Answers

They'll only be equivalent if the first thing after breaking out of the loop is a return statement.

In any case, if your lecturer doesn't allow break, they probably won't allow return either.

You should be aware that these rules have a good basis behind them (to prevent spaghetti code and make it easier to write maintainable code) but sometimes they're enforced over-zealously.

For example, there's nothing unreadable about the segment:

if (x == 0)
    return 0;
return x - 1;

even though it has multiple return statements.

The supposedly preferred solution to the no-multiple-return crowd is something like:

int y = x - 1;
if (x == 0)
    y = 0;
return y;

which is, in my opinion, both less readable and a waste of space.

The real problems occur when you have very complex logic and the hoops that people jump through to avoid break and/or return leads to conditionals that are darn-near unreadable.

Listen to your lecturer - they are, after all, the ones in control of what grade you get. Then, once you get into the real world, you can switch from dogmatism to pragmatism and make up your own mind.

See here for some good advice regarding these issues outside of educational institutions.

like image 120
paxdiablo Avatar answered Oct 20 '22 00:10

paxdiablo