Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Flow Control and Exception Handling - Label Statements

Syntax

    identifier:
  • can be used with loops or statement blocks
  • must precede the statement
  • useful with break or continue which normally terminate or continue the innermost block
outer:
    for( i=0; i<10; i++ ){
        for( j=10; j>0; j--){
            if( j == 5 ) {
             break outer;       // exit entire loop
            } 
        }            
    }
     
Output: 0 5     
     
outer:
    for( i=0; i<10; i++ ){
        for( j=10; j>0; j--) {
            if( j== 5 ) {
              continue outer;   // next iteration of i
            }  
        }
    }
    
Output: 10 5        
Note
  • two or more statements can have the same name as long as one is not enclosed within the other

Example Code


Statements if switch for while do
Labels Exceptions Handling Exceptions try-catch-finally