Challenge - 5 Problems
Repeat-while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple repeat-while loop
What is the output of this Swift code using a repeat-while loop?
Swift
var count = 1 repeat { print(count) count += 1 } while count <= 3
Attempts:
2 left
💡 Hint
Remember that repeat-while runs the loop body first, then checks the condition.
✗ Incorrect
The loop starts with count = 1, prints it, then increments. It repeats while count <= 3, so it prints 1, 2, and 3.
❓ Predict Output
intermediate2:00remaining
Repeat-while loop with condition false initially
What will this Swift code print?
Swift
var number = 5 repeat { print(number) number += 1 } while number < 5
Attempts:
2 left
💡 Hint
The repeat-while loop runs the body once before checking the condition.
✗ Incorrect
Even though the condition is false at the start, the loop body runs once and prints 5.
🔧 Debug
advanced2:00remaining
Identify the error in this repeat-while loop
What error does this Swift code produce?
Swift
var i = 0 repeat { print(i) i += 1 } while i = 5
Attempts:
2 left
💡 Hint
Check the condition syntax in the while statement.
✗ Incorrect
The condition uses '=' which is assignment, not comparison. Swift requires '==' for comparison, so this causes a syntax error.
🧠 Conceptual
advanced2:00remaining
Repeat-while loop vs while loop behavior
Which statement correctly describes the difference between repeat-while and while loops in Swift?
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.
✗ Incorrect
repeat-while runs the body first, then checks the condition, so it always runs at least once. while checks the condition first and may skip the body.
❓ Predict Output
expert3:00remaining
Output of nested repeat-while loops
What is the output of this Swift code with nested repeat-while loops?
Swift
var x = 1 repeat { var y = 1 repeat { print("(\(x),\(y))") y += 1 } while y <= 2 x += 1 } while x <= 2
Attempts:
2 left
💡 Hint
The inner loop runs fully for each iteration of the outer loop.
✗ Incorrect
For x=1, y runs 1 to 2 printing (1,1) and (1,2). Then x=2, y runs again 1 to 2 printing (2,1) and (2,2).