Bird
0
0

You want to print numbers from 10 down to 1 using a do-while loop in PHP. Which code snippet correctly achieves this?

hard📝 Application Q8 of 15
PHP - Loops
You want to print numbers from 10 down to 1 using a do-while loop in PHP. Which code snippet correctly achieves this?
A$num = 10; do { echo $num . ' '; $num++; } while ($num > 0);
B$num = 1; do { echo $num . ' '; $num++; } while ($num <= 10);
C$num = 10; do { echo $num . ' '; $num--; } while ($num >= 1);
D$num = 1; do { echo $num . ' '; $num--; } while ($num < 10);
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to print from 10 down to 1, so start at 10 and decrement.
  2. Step 2: Check each option

    $num = 10; do { echo $num . ' '; $num--; } while ($num >= 1); starts at 10, decrements, and loops while >= 1, which matches the goal.
  3. Final Answer:

    $num = 10; do { echo $num . ' '; $num--; } while ($num >= 1); -> Option C
  4. Quick Check:

    Start high, decrement, loop while condition true [OK]
Quick Trick: Count down with decrement and condition >= 1 [OK]
Common Mistakes:
  • Incrementing when counting down
  • Wrong loop condition direction
  • Starting from wrong initial value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes