Bird
0
0

Which of the following code snippets correctly demonstrates a do-while loop in C that prints numbers from 1 to 3?

easy📝 Syntax Q3 of 15
C - Loops
Which of the following code snippets correctly demonstrates a do-while loop in C that prints numbers from 1 to 3?
Aint i = 1; do { printf("%d ", i); i++; } while (i <= 3);
Bint i = 1; while (i <= 3) { printf("%d ", i); i++; }
Cint i = 1; do { printf("%d ", i); i++; } while i <= 3;
Dint i = 1; do { printf("%d ", i); i++; } while i < 3;
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct syntax for a do-while loop

    The do-while loop syntax requires the condition to be inside parentheses and followed by a semicolon.
  2. Step 2: Check each option

    int i = 1; do { printf("%d ", i); i++; } while (i <= 3); uses the correct syntax: do { ... } while (condition);. int i = 1; while (i <= 3) { printf("%d ", i); i++; } is a while loop, not do-while. int i = 1; do { printf("%d ", i); i++; } while i <= 3; and D miss parentheses around the condition, which is a syntax error.
  3. Final Answer:

    int i = 1; do { printf("%d ", i); i++; } while (i <= 3); is the correct syntax.
  4. Quick Check:

    Condition must be in parentheses and end with a semicolon [OK]
Quick Trick: Condition in do-while must be in parentheses and end with semicolon [OK]
Common Mistakes:
  • Omitting parentheses around the condition
  • Forgetting the semicolon after the while condition
  • Confusing do-while with while loop syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes