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.
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.
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.
Joined: Jul 07, 2009
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.
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.