Question 2
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#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!