Bird
0
0

What is the error in the following code snippet?

medium📝 Debug Q6 of 15
C - Loops
What is the error in the following code snippet?
int x = 0;
while (x < 4)
    printf("%d", x);
    x++;
AVariable x is not initialized.
BThe loop condition should be x <= 4 instead of x < 4.
CMissing semicolon after printf statement.
DThe increment statement is outside the while loop block.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop body

    Only the first statement after while is considered inside the loop without braces.
  2. Step 2: Check increment position

    x++ is outside the loop, so x never changes inside the loop.
  3. Step 3: Consequence

    This causes an infinite loop printing 0 repeatedly.
  4. Final Answer:

    The increment statement is outside the while loop block. correctly identifies the error.
  5. Quick Check:

    Increment must be inside loop block [OK]
Quick Trick: Use braces to include all statements inside while loop [OK]
Common Mistakes:
  • Forgetting braces for multiple statements
  • Incorrect loop condition
  • Not initializing loop variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes