LearnFrenzy
Sign in Take a free test

Control Flow Statements · Find the Error · Question 7

Error: break with a label that does not enclose it Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        search:
        for (int i = 0; i < 3; i++) {
            System.out.println(i);
        }
        for (int j = 0; j < 3; j++) {
            if (j == 1) break search;
        }
    }
}
  • A Labels must be uppercase
  • B The label search belongs to the first loop, which does not enclose the break; a labelled break can only target an enclosing statement Correct
  • C j must start at 1
  • D break cannot have a label
Correct Answer: Option B

Labels are not goto targets – they only name the statement they precede.

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