break Statement

Purpose:

When executed in a while, for, do-while, or switch structure, causes immediate exit from that structure.

Mechanics:

break;

Example:

for(int i=0; i<10; i++) {
if (numA == 5) {
break; // if the integer numA, is 5, break out of the for loop
}
/* other Java statement */
}

Usage:

  • Common uses for the break statement are to escape early from a loop, or to skip the remainder of a switch statement.
  • Execution continues with the first line of code after the structure.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu