Nested Iteration

AP Computer Science A· difficulty 4/5

public static int run() {
    int x = 0;
    outer:
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (i + j == 4) break outer;
            x++;
        }
    }
    return x;
}
// Call: System.out.println(run());

What is printed?

  • A

    9

  • B

    4

    check_circle
  • C

    16

  • D

    5

Explanation

i=0: j=0,1,2,3 (x=4), j=4 breaks outer. x = 4.

Want 10 more like this — adaptive to your weak spots?

Related questions