• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Label for the continue/break statement

 
Hina Mustafa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3753
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic