aspose file tools*
The moose likes Beginning Java and the fly likes Disadvantages of break and continue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Disadvantages of break and continue" Watch "Disadvantages of break and continue" New topic
Author

Disadvantages of break and continue

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Hi Friends,

break is used to end the execution of loop and continue is to skip the current iteration of the loop.

May i know the disadvantages of break and continue statements. And in which situation i have to avoid these statements.


N.Senthil Kumar
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Personally, I have never used continue in my loops, and very rarely use break.
The reason I don't like break\continue is that a code using these statements is harder to read and debug.
If I find myself in a situation where I can use break\continue, I prefer to 'break' that code into methods.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I dont think one can list out the advantages & disadvantages of using break/continue. Its just something available in java for program flow control. It depends on the programmer who uses them - it may be used efficiently or not. You also cannot just like that rule out that break/continue are not needed at all.
There of course are some situations which will demand using it, as Java does not have 'Goto Label' kind of program flow controlling options.


OCPJP 6
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Vinoth Kumar Kannan wrote:I dont think one can list out the advantages & disadvantages of using break/continue. [...] it may be used efficiently or not.

So how do you determine if they're being used "efficiently" or not?

Of course there are advantages and disadvantages to using it.

Advantages: Break is the clearest and easiest way to break out of deeply nested loops. Continue is sometimes the clearest way to skip an element while looping.
Disadvantages: Can lead to difficult-to-maintain code, and may make code harder to reason about.

I use both only rarely, but I don't have a rule *against* using them. I even used a labeled continue once, which surprised me (once in ~12 years; it was about two years ago). I think they're fine under tightly-controlled conditions--by keeping method size small (as mentioned above), and keeping any break/continue/labeled stuff very local (in terms of vertical space), they're just as readable or even more so than other options.
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
is any there any possibilities ,if more Break statements used in an Java Application which is running in a web server , may cause the server to crash.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's just a programming construct--that's like asking if too many "if" statements could make your program crash.
Fredrik Sassenberg
Greenhorn

Joined: Jul 08, 2009
Posts: 4
While refactoring or even trying to optimize code using a break or continue can be a quick win.
But I would agree with Leroy, if I find a lot of continue and breaks in code I will start suspecting a bad smell.
It's in the language so there is nothing wrong about using it but I tend to take a step back and think about why and how I ended up using it.
Maybe there is some way I could have solved it differently?

As long as you are clearly defining what you are trying to do in your method and writing your test you should be fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Disadvantages of break and continue