Using break and continue

 

Overview

At times, it is desirable to exit from a loop or short-circuit the current iteration of a loop. The break and continue statements make it happen.

 

The break statement

for (int x = 0; x < 10; x++) {
  if (x == 5)
    break;
  else
    System.out.print("   " + x);
}

will end prematurely when x takes on a value of 5. The output displayed will be:

   0   1   2   3   4

i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2

switch (key) {
  case 'a':
  case 'A':
    System.out.println("You entered an 'A'");
    break;
  case 'b':
  case 'B':
    System.out.println("You entered a 'B'");
    break;
  default:
    System.out.println("You did not enter an 'A' or a 'B'");
    break;
}

 

The continue statement

   0   1   2   3   4   6   7   8   9

i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2
i = 2, j = 5
i = 2, j = 4
i = 2, j = 3
i = 3, j = 5
i = 3, j = 4
i = 4, j = 5

 

Review questions

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

int x = 7;
if (x > 6) {
  break;
  System.out.println("Big number");
}
else {
  System.out.println("Small number");
}

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. the statements will compile but nothing will be displayed

  4. the statements will compile and "Big number" will be displayed

  5. the statements will compile and "Small number" will be displayed

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

while (true) {
  for (int i = 3; i > 0; i--) {
    if (i == 3)
      continue;
    else
      System.out.println(i);
  }
  break;
}
System.out.println("All done");

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. 2
    1
    2
    1
    (in an endless loop)

  4. 2
    1
    All done

  5. All done

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

for (int i = 1; i <= 3; i++) {
  for (int j = 3; j >= 1; j--) {
    if (i != j)
      continue;
    else
      System.out.println("i = " + i + ", j = " + j);
  }
}

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. i = 1, j = 1
    i = 2, j = 2
    i = 3, j = 3

  4. i = 3, j = 3

  5. the statements will compile but nothing will display

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

for (int i = 1; i <= 3; i++) {
  for (int j = 3; j >= 1; j--) {
    if (i != j)
      break;
    else
      System.out.println("i = " + i + ", j = " + j);
  }
}

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. i = 1, j = 1
    i = 2, j = 2
    i = 3, j = 3

  4. i = 3, j = 3

  5. the statements will compile but nothing will display