LearnFrenzy
Sign in Take a free test

Control Flow Statements · Find the Error · Question 1

Error: a statement after break Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 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) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                break;
                System.out.println("stopping at 3");
            }
        }
    }
}
  • A break needs a label
  • B The println comes directly after break in the same block, so it can never run – javac reports an unreachable statement Correct
  • C i must be final
  • D for loops cannot contain if
Correct Answer: Option B

Move the println before break.

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