File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of Break and Continue Statements. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Use of Break and Continue Statements." Watch "Use of Break and Continue Statements." New topic
Author

Use of Break and Continue Statements.

deepu Bhalotia
Ranch Hand

Joined: Apr 19, 2005
Posts: 39
Where should i use break and continue statements.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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

Joined: Apr 25, 2005
Posts: 50
That Explanation seems to be good.


Cheers,<br />Jagadesh.<br /> <br />--------------------------------<br />SCJP 1.4 , SCBCD 1.3 , SCWCD 1.4<br />--------------------------------
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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
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
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
can u explain with some code
Jagadesh cs
Ranch Hand

Joined: Apr 25, 2005
Posts: 50
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
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
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Break and Continue Statements.