How do I use the break and continue statements?
Author: Deron Eriksson
Description: This tutorial describes how to use the Java break and continue statements.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


When looping in JavaSW, the break and continue statements can come in handy. The continue statement lets you jump out of the current iteration of a loop and then continue with the next iteration.

We can see this illustrated in the following code:

...
	System.out.println("continue when i is 2:");
	for (int i = 1; i <= 3; i++) {
		if (i == 2) {
			System.out.print("[continue]");
			continue;
		}
		System.out.print("[i:" + i + "]");
	}
...

The output of the above code is shown here. Notice that the code after the continue statement isn't executed after the second iteration, but that we still execute the third iteration.

continue when i is 2:
[i:1][continue][i:3]

The break statement breaks out of a loop. It jumps out of the current iteration and ends the immediate loop.

...
	System.out.println("\nbreak when i is 2:");
	for (int i = 1; i <= 3; i++) {
		if (i == 2) {
			System.out.print("[break]");
			break;
		}
		System.out.print("[i:" + i + "]");
	}
...

The above code generates the following output. We break out of the second iteration and break out of the loop, so the code after the break statement during the second iteration isn't executed, and we don't execute the third iteration.

break when i is 2:
[i:1][break]

When using the break and continue statements, it is possible to break or continue to a label. This is as close to a goto statement as Java gets. Breaking and continuing to labels is useful when you have nested loops.

The following code has nested loops and breaks to the label of the outer loop.

...
	System.out.println("\nbreak to outer when i is 2 and j is 2:");
	here: for (int i = 1; i <= 3; i++) {
		for (int j = 1; j <= 3; j++) {
			if ((i == 2) && (j == 2)) {
				System.out.print("[break to outer]");
				break here;
			}
			System.out.print("[i:" + i + ",j:" + j + "]");
		}
	}
...

The output is shown here. Notice that breaking to the outside loop effectively terminates both loops.

break to outer when i is 2 and j is 2:
[i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][break to outer]

Below, we can see an example of breaking to an inner loop.

...
	System.out.println("\nbreak to inner when i is 2 and j is 2:");
	for (int i = 1; i <= 3; i++) {
		here2: for (int j = 1; j <= 3; j++) {
			if ((i == 2) && (j == 2)) {
				System.out.print("[break to inner]");
				break here2;
			}
			System.out.print("[i:" + i + ",j:" + j + "]");
		}
	}
...

The code above produces the following output. Notice that breaking to the inner label terminates the current inner loop, and that we then proceed with the next iteration of the outer loop.

break to inner when i is 2 and j is 2:
[i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][break to inner][i:3,j:1][i:3,j:2][i:3,j:3]

Next, we can see an example of continuuing to an outer loop label.

...
	System.out.println("\ncontinue to outer when i is 2 and j is 2:");
	here3: for (int i = 1; i <= 3; i++) {
		for (int j = 1; j <= 3; j++) {
			if ((i == 2) && (j == 2)) {
				System.out.print("[continue to outer]");
				continue here3;
			}
			System.out.print("[i:" + i + ",j:" + j + "]");
		}
	}
...

The output of the above code is shown here. We can see that continuing to the outer loop label ends the current inner loop and that we then proceed with the next iteration of the outer loop.

continue to outer when i is 2 and j is 2:
[i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][continue to outer][i:3,j:1][i:3,j:2][i:3,j:3]

Here, we can see continuing to an inner loop label.

...
	System.out.println("\ncontinue to inner when i is 2 and j is 2:");
	for (int i = 1; i <= 3; i++) {
		here4: for (int j = 1; j <= 3; j++) {
			if ((i == 2) && (j == 2)) {
				System.out.print("[continue to inner]");
				continue here4;
			}
			System.out.print("[i:" + i + ",j:" + j + "]");
		}
	}
...

From the output below, we can see that continuing to an inner loop label skips the code of the current iteration of the inner loop below the continue statement, but then we execute the next iteration of the inner loop.

continue to inner when i is 2 and j is 2:
[i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][continue to inner][i:2,j:3][i:3,j:1][i:3,j:2][i:3,j:3]