LearnFrenzy
Sign in Take a free test

C Loops · Find the Error · Question 2

Semicolon after while (…) – infinite loop Answer, explanation & discussion

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

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

main.c
#include <stdio.h>

int main(void) {
    int i = 0;
    while (i < 3);
    {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
  • A The braces cannot follow a while
  • B The ; after while (i < 3) is the loop body, so the loop repeats an empty statement forever and never reaches the block Correct
  • C i must start at 1
  • D There is no bug; it prints 0 1 2
Correct Answer: Option B

The block below is not part of the loop. Since the empty body never changes i, i < 3 stays true and the program hangs. Remove the semicolon.

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