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 - while Statement

Syntax (JLS §14.12)

    do {
            statement(s);
    } while(boolean expression);
  • executes statement(s) until expression is false
  • statement(s) are always executed at least once
    do {
        System.out.println("Always executed at least once");
    } while( false );       
  • break can be used to alter do-loop processing
    do {
         if( i==6 ) break;          // exit loop
         System.out.println(i);
         i++;
    }while( i < 10 );
  • continue can be used to alter do-loop processing
    do {
    
         if( i==6 ) {
             i--;
             continue;       // skip 6
         } else {
             System.out.println(i);
             i--;
         }

    } while ( i >= 0);     

Example Code


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