• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping an inner while loop  RSS feed

 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the rule of break is to never use it unless it's in a switch statement, how should I stop the inner loop in the code below?
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally keyword "break" is use to terminate the loop iteration at some point or also in the switch statement to terminate the execution further involving other case statements.

So, stopping an inner while loop, you can obviously use break.

What is the exact problem???
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buenny Fry wrote:the rule of break is to never use it unless it's in a switch statement

What is this 'rule of break'? And what is source behind it?
Buenny Fry wrote:This works perfectly but is discouraged

Why?
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:
Buenny Fry wrote:the rule of break is to never use it unless it's in a switch statement

What is this 'rule of break'? And what is source behind it?
Buenny Fry wrote:This works perfectly but is discouraged

Why?


This should answer yours and Gaurangkumar's questions.
In the course I am taking I am suppose to follow the java style guide documented here: http://www.javaranch.com/style.jsp

"Never use break other than in a switch statement"

That statement is my exact problem why I cannot use break to accomplish my goal.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, frankly, I don't see much problem in your code (at least I don't see any absolute necessity of not using break inside a loop).

Apart from that, I'm not much convinced about that 'style' (e.g. Never use 'do-while', never use 'continue' et-cetera).

IMHO, following SUN/Oracle's official coding style is enough.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java language gives you all the room you need to write code that would be very difficult for others to understand. Java also permits you to write code that is very easy to understand. Most development teams would prefer the latter.

A style guide provides provides a map so that the code generated by a group of programmers will be consistent and, therefore, easier to read and maintain.


It depends upon you and your development team to use a study guide or not...

If you want than do the coding in that way...

In your case you can use cascaded switch construct if you want to replicate your program...
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Well, frankly, I don't see much problem in your code (at least I don't see any absolute necessity of not using break inside a loop).

Apart from that, I'm not much convinced about that 'style' (e.g. Never use 'do-while', never use 'continue' et-cetera).

IMHO, following SUN/Oracle's official coding style is enough.


Well, I can't agree nor disagree with you about that because I don't know java well enough to form my own opinions about them. However, I must follow the rules of my class and find an alternative to using break in order to receive full credit for this assignment.
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:In your case you can use cascaded switch construct if you want to replicate your program...

I just might test out that route. Thanks.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I never use break and continue in loops. instead I use flags...
for instance,
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, the "rule" is a good one to follow. The motivation behind the rule is clarity. When you break out of a loop, especially one that is nested a few levels down, it can make it difficult to follow the logical flow of execution thus reducing the clarity of the code. Unclear code is the breeding ground of hard-to-find bugs in your programs.

That said, I usually try to solve the problem of poor code clarity first. The compose method refactoring is my goto technique to deal with this. After I have composed to crystal clear clarity, I'll see if the refactored code still requires the use of break. Usually it becomes a trivial matter to either return a boolean instead or get rid of the break altogether.

Notice in the example of the Compose method refactoring I cited, there's another common "rule" that is broken: "A method should have only one exit point: do not use return in multiple places in a method." Again, this is a good rule of thumb but in the example for Compose method, the return in the guard clause is very clear and does no harm to the clarity of the rest of the method.

IMO, "rules" like these are good as general guidelines but can be justifiably broken under certain conditions.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:IMO, "rules" like these are good as general guidelines but can be justifiably broken under certain conditions.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not saying this is good or bad style, but you can have multiple conditions in your while. Pseudo code:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:Personally, I never use break and continue in loops. instead I use flags...

Personally, I never use "never" and I've seen more cases where a bug has been caused by an incorrectly set flag than by using break; although I entirely agree with the other warnings given here.

Basically, I agree with Junilu: the main thing you should strive for is clarity. If (as in Fred's example) you can do it by adding an extra condition to your loop, do it; but there are occasions where that may involve quite a lot of '&&'s or '||'s to plough through, and in those cases I sometimes just wait for them to occur naturally in the loop code and issue a break where appropriate.

Another variant, beloved of old procedural bods like me is the Dijkstra-style loop:eg:
Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Another variant, beloved of old procedural bods like me is the Dijkstra-style loop:eg:


This is how I was taught to write a loop, but I find it so infrequently in the code of my peers that I had begun to wonder if there were something wrong with me.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:but I find it so infrequently in the code of my peers that I had begun to wonder if there were something wrong with me.

Nothing wrong; just the same as me - annually challenged.

Winston
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your responses and suggestions. I finally decided to just bend the rule and use that solitary break keyword to exit my inner loop. I know break should be reserved for switch statements, but to accomplish my goal any other way would have involved redoing some of my already perfectly working code ... and I think that would be pointless. Why add more code to something that can be accomplished with a single code word? It just didn't make sense and in certain cases like this I think it should be okay to bend the rules a little.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!