break can be used inside a loop or a switch construct, whereas a continue can be used inside a loop.
when we use break the control comes out from the block of code in which it resides. when we use a continue the code following is ignored and is not executed and instead the next iteration is started. Again if we use continue inside a switch construct which is inside a loop, continue can directly be followed by some code(i mean we can afford to write some code after continue statement directly), which is not poosible otherwise, without inlusion of a '}' (closing brace) in b/w next line and continue stmt. If we try to write sth directly after continue, we have that line of code unreachable and we get an errror A similar kinda behavoir is with 'break' and 'return' clause also.......
plz feel free to make any amendments anyone.......
hi amit i didn't understand this part in ur explanation.
Again if we use continue inside a switch construct which is inside a loop, continue can directly be followed by some code(i mean we can afford to write some code after continue statement directly), which is not poosible otherwise, without inlusion of a '}' (closing brace) in b/w next line and continue stmt. If we try to write sth directly after continue, we have that line of code unreachable and we get an errror A similar kinda behavoir is with 'break' and 'return' clause also
Joe Sondow
Ranch Hand
Joined: Apr 10, 2005
Posts: 195
posted
0
Another way break can be used is to exit a labelled block, as in this example:
The output is ABD
The code block surrounded by braces is labelled with the name "foo". You can't use a break statement outside a loop or switch statement UNLESS the break statement specifies a label that correspondends to the block where your break statement resides. In the above example, the break statement specifies the label "foo" which is the label for the block where the break statement resides.
The reason I put the "if (true)" clause is because otherwise the code would not compile because "System.out.print("C");" would be considered an unreachable statement.
SCJA 1.0 (98%), SCJP 1.4 (98%)
Jagadesh cs
Ranch Hand
Joined: Apr 25, 2005
Posts: 50
posted
0
Hi parameswaran
Hope you got the answer for your question from joe's explanation.
Similar to the break he had used in his code , if you use continue followed by a statement , that would be unreachable and it wont compile so you have to put continue just like the break inside a if or after a } (closed brace )
Such things are not needed if continue is used in a switch within a loop. This was the actual explanation by amit
hope this helps !
Parameswaran Thangavel
Ranch Hand
Joined: Mar 01, 2005
Posts: 485
posted
0
can u explain with some code
Jagadesh cs
Ranch Hand
Joined: Apr 25, 2005
Posts: 50
posted
0
You can try with joe's code .. just include a for or while loop after the label foo, replace break by continue and remove the if()
some thing like this
Compile this and see what happens, then include an if loop before continue
now see what happens .
You can also include continue in a pair of braces ( { and }) and compile instead of using if .
hope this helps
Parameswaran Thangavel
Ranch Hand
Joined: Mar 01, 2005
Posts: 485
posted
0
hi on givinvg the braces it throws unreachable error.
class S { public static void main(String[] args) { System.out.print("A");
foo: for ( int i=0; i< 2; i++) { System.out.print("B"); { continue foo; } System.out.print("C"); }
} }
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Given the use of the word should, the answer is never. The use of break or continue merely means you need to learn how to write your application logic more precisely. It's a common misunderstanding and, therefore, I can foresee the bazillions of responses now.
Did you mean 'should'? This is SCJP, which has nothing to do with how to design software.
Originally posted by Tony Morris: Given the use of the word should, the answer is never. The use of break or continue merely means you need to learn how to write your application logic more precisely. It's a common misunderstanding and, therefore, I can foresee the bazillions of responses now.
Did you mean 'should'? This is SCJP, which has nothing to do with how to design software.
I totally agree. When writing high-quality code for a useful application, you should avoid using break and continue altogether.
In the context of this SCJP message board, I suspect that the original post, "Where should i use break and continue statements" was an attempt to learn where break and continue statements should go in order to avoid compilation errors, which is a valid question.