LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 10

A labelled continue in nested loops Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        outer:
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                if (j > i) continue outer;
                if (i * j == 6) break outer;
                sb.append(i).append(j).append(' ');
            }
        }
        System.out.println(sb.toString().trim());
    }
}
  • A 11 21 22 31 Correct
  • B 11 21 22 31 32
  • C 11 21 22 31 32 33
  • D 11 22 33
Correct Answer: Option A

continue outer ends a row as soon as j > i. At i = 3, j = 2 the product is 6 and break outer stops everything.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links