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
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.