The Java break statement and its friend continue can also be used to cut short a loop or conditional statement by jumping out of it. A break causes Java to stop the current block statement and resume execution after it. In the following example, the while loop goes on endlessly until the condition() method returns true, triggering a break statement that stops the loop and proceeds at the point marked “after while.”
 while( true ) {  
 if ( condition() )  
 break;  
 }  
 // after while  
 for( int i=0; i < 100; i++ ) {  
 if ( i == 33 )  
 continue;  
 System.out.println( i );  
 }  
labelOne:
 while ( condition ) {  
 ...  
 labelTwo:  
 while ( condition ) {  
 ...  
 // break or continue point  
 }  
 // after labelTwo  
 }  
 // after labelOne  






 

 
 
 
 
 
0 comments:
Post a Comment