LearnFrenzy
Sign in Take a free test

C Loops · Find the Error · Question 7

Error: break outside a loop or switch Answer, explanation & discussion

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

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int age = 15;
    if (age < 18) {
        puts("minor");
        break;
    }
    puts("checked");
    return 0;
}
  • A puts cannot be used in an if
  • B break is only allowed inside a loop or a switch, not to leave an if Correct
  • C age must be unsigned
  • D There is no bug
Correct Answer: Option B

break leaves loops and switches; an if just ends at its closing brace. Use else, or return from the function.

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