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 Label for the continue/break statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Label for the continue/break statement " Watch "Label for the continue/break statement " New topic
Author

Label for the continue/break statement

Hina Mustafa
Greenhorn

Joined: Dec 01, 2000
Posts: 8
A label for the continue /break statement can be before a for, do or while loop (other than white space(s) and comments)
Can it be used
1 before if or else
2 before another label
3 before an opening brace like '{'
4 anywhere in a method
thanx
Amond Adams
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
continue and break with labels are workable only in the case of loops (when you want to name loops). Though these two can be used without labels in loops too (where then they relate to immediate context (loop))
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
Just answered a similar one on Marcus's site! http://www.jchq.net/discus/messages/1/20419.html?975801381
(Heading: Mock exam question)
1,2 and 4 are not valid.
FYI, this is a valid code snippet:
lab: {
break lab;
}
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
we can use the label before 'if'
Here is an example
class label {
public static void main(String s[]){
boolean x=true;int i=0;boolean y=true;
for ( int f=0;f<=10 ;f++ )
{ System.out.println("for loop value"+f);

label:if (x==true){ System.out.println("start");
while ( y ) { i=(int)(10*Math.random());
System.out.println(i);
if ( i<4 ){x=true;break label;}
if ( i==9 ){x=false; break label;}
}//end of while
} //end of if
}//end of for
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
To make the use of break's Label
(before 'if' ) more reasonable I made little bit changes with my above example.Here it is

class label {
public static void main(String s[]){
boolean x=true;int i=0;boolean y=true;
for ( int f=0;f<=10 ;f++ )
{

label: if (x==true){ System.out.println("________start");
while ( y ) { i=(int)(10*Math.random());
System.out.println(i);
if ( i<4 ){x=true;break label;}//Line1
if ( i==9 ){x=false; break label;}//Line2
}//end of while
} //end of if
System.out.println("for loop value was"+f); }//end of for
}
}
but one thing, we can't use continue like that if you replace
break with continue on Line1 or line2 there will be error.
[This message has been edited by Nasir Khan (edited December 03, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Label for the continue/break statement