donough sugrue
donough sugrue

Reputation: 21

Labelled break statement...where next

I'm just doing the Sierra-Bates SCJP study guide and am wondering about the following question:

public class Wind {

  public static void main(String[] args) {

    foreach:
      for(int j=0; j<5; j++) {
        for(int k=0; k< 3; k++) {

          System.out.print(" " + j);
          if(j==3 && k==1) break foreach;      //1
          if(j==0 || j==2) break;

        }
      }

  }
}

When the value of k is equal to 1 and break foreach is executed (at line 1), where exactly does iteration go after this, to my mind does this now exit the whole loop. Where is the next point of iteration ? How is there any further iterations if the outside loop is exited ? The answer given in the study book for this is 0111233. The way I see it, only 01 gets printed before the entire loop is exited. No ?

Upvotes: 1

Views: 338

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213233

where exactly does iteration go after this, to my mind does this now exit the whole loop

Yes, since you are breaking out of outer loop. So, it will exit the whole loop you can say.

Where is the next point of iteration?

There is not iteration after you break out of your outer loop.

How is there any further iterations if the outside loop is exited ?

No, there will not be any further iteration. Why would you expect any further iteration? When you break out of a loop, then the further iteration of that loop will not run.

For getting the output, try stepping through your code step by step. Take a paper and pencil out, and for each iteraion write the value of j and k, and see which if is getting executed each time.

Note that, your 2nd if is only breaking out of the inner for loop.

Here are your two if conditions: -

  if(j==3 && k==1) break foreach;      // 1
  if(j==0 || j==2) break;              // 2

Iterations: -

1). j = 0, k = 0: - Prints 0. First if fails, second if succeeds. You break out of inner loop. Continue with outer (j++)

2). j = 1, k = 0: - Prints 1. First if fails, second if fails. Continue inner loop (k++)

3). j = 1, k = 1: - Prints 1. Both if fails. Continue inner loop (k++)

4). j = 1, k = 2: - Prints 1. Both if fails. Continue inner loop (k++)

5). j = 1, k = 3: - Inner loop condition fails. Continue outer loop (j++, k = 0)

6). j = 2, k = 0: - Prints 2. First if fails. Second if succeeds. Break inner loop. Continue outer loop (j++)

7). j = 3, k = 0: - Prints 3. Both if fails. Continue inner loop. (k++)

8). j = 3, k = 1: - Prints 3. First if succeeds. Breakes out of outer loop. Iteration ends.

Now see, what are the print statements: -

0111233

Upvotes: 1

Mel Nicholson
Mel Nicholson

Reputation: 3225

This may be a little easier to see with better printing:

public class Temp {
  public static void main(String[] args) {
    foreach: for (int j = 0; j < 5; j++) {
      System.out.println("Outer(j=" + j + ")");
      for (int k = 0; k < 3; k++) {
        System.out.println("Inner (j=" + j + "; k=" + k +")" );
        if (j == 3 && k == 1) break foreach; // 1
        if (j == 0 || j == 2) break;
      }
      System.out.println("Outer - after inner loop");
    }
    System.out.println("Not in a loop");
  }
}

The general rule is that a break statement moves execution to the first statement outside the loop which follows it. That can be a little confusing when the loop is itself the last statement in another loop's body, because the statement following the inner loop is the loop mechanism of the outer loop (i.e. the test of a while loop or the action-and-test of a for loop)

The unqualified break statement exits the inner loop and then finishes the current iteration of the outer loop (where it says "after inner loop").

The qualified break statement exits the outer loop and goes to the first statement following the outer for loop.

This results in the following output.

Outer(j=0)
Inner (j=0; k=0)
Outer - after inner loop
Outer(j=1)
Inner (j=1; k=0)
Inner (j=1; k=1)
Inner (j=1; k=2)
Outer - after inner loop
Outer(j=2)
Inner (j=2; k=0)
Outer - after inner loop
Outer(j=3)
Inner (j=3; k=0)
Inner (j=3; k=1)
not in a loop

Upvotes: 0

Related Questions