C language – while loop syntax sanity check: Point out the compile-time error (if any) in this loop with a missing condition. #include <stdio.h> int main() { int i = 1; while() { printf("%d ", i++); if (i > 10) break; } return 0; }

C# Programming Control Instructions Difficulty: Easy
Choose an option
  • A
    There should be a condition in the while loop
  • B
    There should be at least a semicolon in the while
  • C
    The while loop should be replaced with for loop.
  • D
    No error
  • E
    None of the above

Answer

Correct Answer: There should be a condition in the while loop

Explanation

Introduction / Context:This item tests your grasp of the required syntax for a while loop in C. The loop header must contain a parenthesized expression that can be evaluated to determine whether the next iteration should execute.

Given Data / Assumptions:

  • The loop is written as while() with empty parentheses.
  • Body prints i and conditionally breaks.

Concept / Approach:In C, while (expression) requires a valid scalar expression. An empty condition is a syntax error and will not compile. To create an intentional infinite loop, write while(1) (or for(;;)) rather than leaving the condition blank.

Step-by-Step Solution:Parse header: while() → missing expression → syntax error.Fix: add a condition such as while (i <= 10) or while (1).Keep the break to terminate if needed.

Verification / Alternative check:Compiling will emit an error like “expected expression before ‘)’ token.” Changing to while(1) or a proper condition compiles.

Why Other Options Are Wrong:At least a semicolon — a semicolon after while creates an empty loop, not a fix here. Replace with for — unnecessary; while works when written correctly. No error — incorrect.

Common Pitfalls:Accidentally leaving conditions blank; confusing deliberate empty loops (while(x--);) with missing conditions.

Final Answer:There should be a condition in the while loop

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion