The key point in this example is that break statements can be labeled, that is, a break can designate a labeled loop. Specifying "break outer" in the above example terminates the loop labeled "outer". In other words, the break statement terminates both loops.
The same idea applies to continue statements, for example:
public class ControlDemo2 {
public static void main(String args[]) {
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
if (i == 2 && j == 2) {
continue outer;
}
}
}
}
}
Output here is:
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
Break statements are normally used in loop and switch statements, but you can also use them in any labeled block. Here's an example that illustrates this idea:
public class ControlDemo3 {
// add two numbers together,
// a >= 0 and b >= 0
// throw IllegalArgumentException
// if a or b out of range
static int add(int a, int b) {
block1: {
if (a < 0) {
break block1;
}
if (b < 0) {
break block1;
}
return a + b;
}
throw new IllegalArgumentException(
"a < 0 || b < 0");
}
public static void main(String args[]) {
// legal case
try {
int a = 37;
int b = 47;
int c = add(a, b);
System.out.println(c);
}
catch (IllegalArgumentException e) {
System.err.println(e);
}
// illegal case
try {
int a = 37;
int b = -47;
int c = add(a, b);
System.out.println(c);
}
catch (IllegalArgumentException e) {
System.err.println(e);
}
}
}
In this example there's a block labeled "block1". The program handles errors by breaking out of the block. If there are no errors, the program returns normally from within the block. An error causes an exception to be thrown after the block is exited. Note in this example that there are other ways of structuring the code. For example, you might simply say:
if (a < 0 || b < 0) {
throw new IllegalArgumentException(
"a < 0 || b < 0");
}
return a + b;
Which approach is "correct" depends a lot on the complexity of the logic, and what style you prefer.
The final example illustrates the case where you'd like to perform some actions, and then somehow gain control for cleanup processing. You want to do this whether the actions succeed, fail, or trigger an exception. This case is sometimes implemented in C/C++ by using a goto to jump to the end of a function, where there is some cleanup code.
Here's an example of how you can do this using a JavaTM program:
public class ControlDemo4 {
// add two numbers together,
// a >= 0 and b >= 0
// throw IllegalArgumentException
// if a or b out of range
static int traceadd(int a, int b) {
try {
if (a < 0 || b < 0) {
throw new IllegalArgumentException(
"a < 0 || b < 0");
}
return a + b;
}
finally {
System.out.println("trace:
leaving traceadd");
}
}
public static void main(String args[]) {
// legal case
try {
int a = 37;
int b = 47;
int c = traceadd(a, b);
System.out.println(c);
}
catch (IllegalArgumentException e) {
System.err.println(e);
}
// illegal case
try {
int a = 37;
int b = -47;
int c = traceadd(a, b);
System.out.println(c);
}
catch (IllegalArgumentException e) {
System.err.println(e);
}
}
}
This example does program tracing. It prints a message when the traceadd method exits. The exit can be normal, through the return statement, or abnormal, through an exception. Using try...finally (no catch) like this:
try {
statement 1
statement 2
statement 3
...
}
finally {
cleanup
}
is a way to get control for cleanup, no matter what happens in the try clause.
GOTO STATEMENTS AND JAVATM PROGRAMMING
Suppose you write a C/C++ program that searches a 5 x 5 array to find the first occurrence of a particular value. You might use the following approach:
/* iterate through the array,
looking for the target */
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (vec[i][j] == TARGET) {
found = 1;
goto done;
}
}
}
done:
if (found) {
printf("Found at %d %d\n", i, j);
}
return 0;
}
If you run the program, you get the result:
Found at 3 4
In this example, a loop nested in another loop is used to find the matching array element. If the program finds the element, it needs to "break" from the nested loops. It's not sufficient to simply break from the inner loop. Doing that only takes the program to the outer loop, it does not actually terminate both loops. So a goto is used to jump out of the inner loop and transfer control to the "done:" label. Using a goto is not the only way to solve the problem in C/C++, but this is one place where a goto is sometimes used.
Goto statements are controversial. One problem is that it's hard to control the program logic effectively if you use these statements. For example, look again at the program above. It's clear that the "found" test that is just after the "done:" label is intended for use after the loop has terminated (that is, after the loop terminates normally or through the goto). But there's no way to enforce this rule; control can be transferred to this label from anywhere in the function.
In the JavaTM programming language, goto is a reserved word; the Java programming language does not have a goto statement. However there are alternative statements that you can use in the Java programming language in place of the goto statement. This tip demonstrates three alternative statements.
The first of these is a rewrite of the above program:
public class ControlDemo1 {
// 5 x 5 array of numbers
static int vec[][] = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5, 6},
{3, 4, 5, 6, 7},
{4, 5, 6, 7, 8},
{5, 6, 7, 8, 9}
};
static final int N = 5;
// target number to be searched for
static final int TARGET = 8;
public static void main(String args[]) {
int i = 0;
int j = 0;
boolean found = false;
// iterate through the array,
// looking for the target
outer:
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (vec[i][j] == TARGET) {
found = true;
break outer;
}
}
}
if (found) {
System.out.println("Found at " + i +
" " + j);
}
}
}