Bird
0
0

You want to create a repeat-while loop that prints numbers from 10 down to 1. Which code snippet correctly achieves this?

hard📝 Application Q8 of 15
Swift - Loops
You want to create a repeat-while loop that prints numbers from 10 down to 1. Which code snippet correctly achieves this?
Avar num = 1 repeat { print(num) num += 1 } while num <= 10
Bvar num = 1 repeat { print(num) num -= 1 } while num <= 10
Cvar num = 10 repeat { print(num) num += 1 } while num > 0
Dvar num = 10 repeat { print(num) num -= 1 } while num > 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify counting direction

    We want to count down from 10 to 1, so num should start at 10 and decrease.
  2. Step 2: Check loop condition and decrement

    var num = 10 repeat { print(num) num -= 1 } while num > 0 starts at 10, prints num, then decreases num by 1, looping while num > 0, which prints 10 to 1.
  3. Final Answer:

    var num = 10 repeat { print(num) num -= 1 } while num > 0 -> Option D
  4. Quick Check:

    Counting down loop with correct condition and decrement [OK]
Quick Trick: Count down: start high, decrement, loop while > 0 [OK]
Common Mistakes:
  • Incrementing when counting down
  • Starting from 1 instead of 10
  • Wrong loop condition direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes