Recall & Review
beginner
What is a repeat-while loop in Swift?
A repeat-while loop runs the code inside its block at least once, then keeps repeating as long as the condition is true. It's like doing something first, then checking if you should do it again.
Click to reveal answer
beginner
How does a
repeat-while loop differ from a while loop?A
repeat-while loop runs the code block first, then checks the condition. A while loop checks the condition first before running the code. So, repeat-while always runs at least once.Click to reveal answer
beginner
Write a simple Swift
repeat-while loop that counts from 1 to 3.var count = 1
repeat {
print(count)
count += 1
} while count <= 3
This prints 1, 2, and 3 each on a new line.
Click to reveal answer
intermediate
Why might you choose a
repeat-while loop over a while loop?Use
repeat-while when you want the code to run at least once no matter what, like asking a user for input and then checking if it’s valid.Click to reveal answer
beginner
What happens if the condition in a
repeat-while loop is false right after the first run?The loop stops after the first run because the condition is false. But the code inside still runs once before stopping.
Click to reveal answer
What is guaranteed about the code inside a
repeat-while loop?✗ Incorrect
A repeat-while loop always runs the code inside once before checking the condition.
Which keyword starts a repeat-while loop in Swift?
✗ Incorrect
The repeat-while loop in Swift starts with the keyword 'repeat'.
When does the condition get checked in a repeat-while loop?
✗ Incorrect
The condition is checked after the loop body runs in a repeat-while loop.
What will this code print?
var x = 5
repeat {
print(x)
x -= 1
} while x > 3
✗ Incorrect
It prints 5 first, then 4, then stops because x becomes 3 which is not > 3.
If the condition is false at the start, how many times does a repeat-while loop run?
✗ Incorrect
The loop runs once before checking the condition, so it runs once even if the condition is false initially.
Explain how a repeat-while loop works and when you would use it.
Think about doing something first, then checking if you should do it again.
You got /3 concepts.
Write a Swift repeat-while loop that prints numbers from 1 to 5.
Start with a number 1, print it, add 1, and repeat while number is less or equal to 5.
You got /4 concepts.