break and continue in Java

The statements break and continue in Java alter the normal control flow of compound statements. The break and continue statements do not make sense by themselves. Without labels (see below), break and continue refer to the most closely enclosing for, while, do, or switch statement. With labels the break statement is legal in a labled if statement or a labeled {} block.

Here is a simple example of a loop that stops searching as soon as a non-zero element is found.

int i = 0;
while (i<max) {
  if (Matrix[i] != 0) break;
  i++;
}
// either Matrix[i]!=0 or i==max

The break statement immediately jumps to the end (and out) of the appropriate compound statement. The continue statement immediately jumps to the next iteration (if any) of the appropriate loop. A continue statement does not apply to a switch statement or a block statement, only to compound statements that loop: for, while, and do. See the example Block.java.

Java does not have a general goto statement. But the statements break and continue take the place of most of uses of the goto. Java does allow any statement to be labeled as in

label : statement
This is useful only for those compound statements, because break and continue can target a labeled compound statement. In this case they take the form:
break label
continue label
The action of break and continue is relative to the same-labeled compound statement regardless of whether or not it is the most closely enclosing compound statement or not. This is different from the unlabeled case only when compound statements are nested inside one another. Labeled break and continue statements are legal only when lexically enclosed by an appropriate compound statement with the same label.

The program Break.java is a simple main program that does nothing useful except illustrate the use of break and continue in Java. Without labels, break and continue are identical to their counterparts in C and C++. With labels the statements can refer to statically enclosing compound constructs.

Compiling the program Break.java yields the Java byte-code file Break.class. Running the program yields the following results:
point A.  'break' on iteration i=1
point B.  j=1

point D.  'continue' on iteration i=1
point E.  j=1
point E.  j=2
point F.  j=2
point E.  j=3
point F.  j=3

point G.  'for'/'switch' i=1
point H.  j=1
point I.  j=1
point H.  j=2
point I.  j=2
point H.  j=3
point I.  j=3

point J.  i=1


point A.  'break' on iteration i=2
point B.  j=1
point C.  j=1
point B.  j=2

point D.  'continue' on iteration i=2
point E.  j=1
point F.  j=1
point E.  j=2
point E.  j=3
point F.  j=3

point G.  'for'/'switch' i=2
point H.  j=1
point H.  j=2
point H.  j=3

point J.  i=2


point A.  'break' on iteration i=3
point B.  j=1
point C.  j=1
point B.  j=2
point C.  j=2
point B.  j=3

point D.  'continue' on iteration i=3
point E.  j=1
point F.  j=1
point E.  j=2
point F.  j=2
point E.  j=3

point G.  'for'/'switch' i=3
point H.  j=1

point J.  i=3


point A.  'break' on iteration i=4
point B.  j=1
point C.  j=1
point B.  j=2
point C.  j=2
point B.  j=3
point C.  j=3

point D.  'continue' on iteration i=4
point E.  j=1
point F.  j=1
point E.  j=2
point F.  j=2
point E.  j=3
point F.  j=3

point G.  'for'/'switch' i=4
point H.  j=1
point H.  j=2
point H.  j=3

point J.  i=4

Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Wed Jan 17 15:54:09 EST 2007