Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Break and Continue Statements.

 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where should i use break and continue statements.
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......

thanx
amit
 
Jagadesh cs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That Explanation seems to be good.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jagadesh cs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u explain with some code
 
Jagadesh cs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"deepu"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic