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 the 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 "the "break" statement" Watch "the "break" statement" New topic
Author

the "break" statement

rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Here is a less known fact about the "break" statement.
A break statement with label identifier attempts to transfer control out of enclosing labeled block statement. In this case, the break target need not be a while, do, for, or switch statement. If no labeled statement with identifier as its label encloses the break statement, a compile-time error occurs.
(From JLS 14.13)
To further illustrate ....
<pre>
public class Test {
public static void main ( String [] args ) {
System.out.println ( "Before block" );
block: {
System.out.println ( "Before break" );
if ( args.length == 0 ) {
break block;
}
System.out.println ( "After break" );
}
System.out.println ( "After block" );
}
}
</pre>
Compiles and runs successfully. Note that it doesn't have
any while, do, for, or switch statements.
Results:
>java Test
Before block
Before break
After block
>java Test 1
Before block
Before break
After break
After block
Regards,
--RajSim
[This message has been edited by maha anna (edited June 01, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
it is quite obvious that you are new to java if you felt that you had to post this message as the purpose of break is the opposite of continue which is genarally used with labels
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
welcome...
Since you are new to java. try the ...
Java in General (beginner) form next time.
This form is for people preparing for the SCJP exam(s).
Enjoy the day
Monty

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
monty im not sure if you were refering to me as new to java but let me fill you in im java certified i got a passing score of 98 on the exam this my first time to the site and i relized most of the discussion was ok until i came across this thread i just wanted to let him/her know that it is a common use so tommorow we dont see postings such as you can use i++ in java or a if statement must evaluate to a boolean expression unlike in c
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hello everybody,
PLease, please , please understand the WHOLE purpose of our friendly forum is to share our knowledge with others however small or silly or difficult or anything at all.
As long as the main post almost fits into our forum let us leave that thread here. If it is completely unrelated we will move to other forums. No offence please...
Before I forget Welcome to JavaRanch Defrag!
regds
maha anna
[This message has been edited by maha anna (edited June 01, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
rajsim,
The point to understand from your post is
if there is a break without label stmt, then it must be within a for/while/do-while/switch statements.
if there is a break with label stmt, then there must be an enclosing stmnt with this label.
I wrote a small prog to illustrate the DO'S and DON'TS of break stmnt. Please have a look.
regds
maha anna
<pre>

class Test {
public static void main ( String [] args ){
System.out.println ( "Before block" );
//----------Case 1----------------
block: {
System.out.println ( "Before break" );
if ( args.length == 0 )
//break ;//compile error
break block;//this is ok
System.out.println ( "After break" );
}
System.out.println ( "After block" );

//----------Case 2----------------
for(int i=0; i<10; i++) {
break; //this is ok
}

//----------Case 3----------------
forLabel:for(int i=0; i<10; i++) {
break forLabel; //this is ok
}
//----------Case 4----------------
for(int i=0; i<10; i++) {
//break forLabel; //Compile error 'No label defn found for forLabel'
}
//----------Case 5----------------
justAStmt : System.out.println("Hello everybody!");
{
//break justAStmt; //Compile error 'No label defn found for justAStmt'
}
//----------Case 6----------------

justAnotherStmt :
{
break justAnotherStmt; //This is ok with MANY new lines in bet
}
//----------Case 7----------------
justAnotherStmt :
//This is a comment
{
break justAnotherStmt; //This is ALSO ok with a COMMENT in bet
}
//----------Case 8----------------
outerloop:
for(int i=0; i<10; i++) {
//outerloop: for(int j=0; j<10; j++){//compiler error CAN"T have same
//loopname as one of ENCLOSING loop labels
//}
}
//----------Case 9----------------
outerloop:for(int i=0; i<10; i++) {
innerloop: for(int j=0; j<10; j++){
break outerloop; //Ok you can break outerloop
}
}
//----------Case 10----------------
block: {
outerloop:for(int i=0; i<10; i++) {
innerloop: for(int j=0; j<10; j++){
break block; //Ok you can break outer block
}
}
}
}
}

</pre>

[This message has been edited by maha anna (edited June 01, 2000).]
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
defrag, monty6, I am NOT new to java. This was just to share something with others.
Before I posted this topic, I searched the forum and found no topics on "break" (in the subject line). Now, people looking for examples on usage of "break" can get the excellent illustration (thanks to Maha Anna) of various cases how it can be used.
Regards
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
rajsim,
We really appreciate your thoughtfulness. Thank you for sharing your findings. Keep 'em coming.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the "break" statement