Recall & Review
beginner
What is a while loop in Swift?
A while loop repeats a block of code as long as a condition is true. It checks the condition before running the code each time.
Click to reveal answer
intermediate
How does a
while loop differ from a repeat-while loop in Swift?A
while loop checks the condition before running the code, so it might not run at all. A repeat-while loop runs the code first, then checks the condition, so it runs at least once.Click to reveal answer
beginner
Write a simple Swift while loop that counts from 1 to 5.
var count = 1
while count <= 5 {
print(count)
count += 1
}
Click to reveal answer
beginner
What happens if the condition in a while loop never becomes false?
The loop runs forever, causing an infinite loop. This can freeze or crash your program.
Click to reveal answer
beginner
Why is it important to update variables inside a while loop?
Updating variables changes the condition so the loop can eventually stop. Without updates, the loop might run forever.
Click to reveal answer
What does a while loop do in Swift?
✗ Incorrect
A while loop repeats the code block as long as the condition remains true.
When does a while loop check its condition?
✗ Incorrect
The while loop checks the condition before each iteration.
What will happen if the condition in a while loop is always true?
✗ Incorrect
If the condition never becomes false, the loop never stops.
Which of these is a correct way to stop a while loop?
✗ Incorrect
Updating the variable that controls the condition lets the loop end.
What is the output of this code?
var i = 3
while i > 0 {
print(i)
i -= 1
}
✗ Incorrect
The loop prints 3, then 2, then 1, then stops when i becomes 0.
Explain how a while loop works in Swift and why updating the condition variable inside the loop is important.
Think about what happens if the condition never changes.
You got /3 concepts.
Write a simple Swift while loop that prints numbers from 1 to 5 and explain each step.
Start with a variable set to 1 and increase it each time.
You got /4 concepts.