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?
AStops the program immediately
BRuns code only once
CRuns code after checking the condition once
DRepeats code while a condition is true
✗ Incorrect
A while loop repeats the code block as long as the condition remains true.
When does a while loop check its condition?
AOnly once at the start
BAfter running the loop code
CBefore running the loop code
DIt never checks
✗ Incorrect
The while loop checks the condition before each iteration.
What will happen if the condition in a while loop is always true?
AThe loop runs forever (infinite loop)
BThe loop runs once
CThe loop never runs
DThe program crashes immediately
✗ Incorrect
If the condition never becomes false, the loop never stops.
Which of these is a correct way to stop a while loop?
AChange the condition variable inside the loop
BDo nothing
CUse a for loop instead
DWrite code outside the 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
}
A1 2 3
B3 2 1
C0 1 2
DNo output
✗ 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.
Practice
(1/5)
1. What does a while loop do in Swift?
easy
A. Repeats code while a condition is true
B. Runs code only once
C. Runs code a fixed number of times
D. Stops the program immediately
Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly as long as its condition stays true.
Step 2: Compare options with the definition
Only Repeats code while a condition is true correctly describes repeating code while a condition is true.
Final Answer:
Repeats code while a condition is true -> Option A
Quick Check:
While loop = repeat while true condition [OK]
Hint: While loops run as long as condition is true [OK]
Common Mistakes:
Thinking while loops run a fixed number of times
Confusing while with if statements
Assuming while loops run only once
2. Which of the following is the correct syntax for a while loop in Swift?
easy
A. while condition: { /* code */ }
B. while (condition) do { /* code */ }
C. while { condition } /* code */
D. while condition { /* code */ }
Solution
Step 1: Recall Swift while loop syntax
Swift uses: while condition { code } without parentheses or extra keywords.
Step 2: Check each option
while condition { /* code */ } matches Swift syntax exactly; others have extra keywords or wrong punctuation.
Final Answer:
while condition { /* code */ } -> Option D
Quick Check:
Swift while syntax = while condition { } [OK]
Hint: Swift while loops use no parentheses around condition [OK]
Common Mistakes:
Adding parentheses around the condition
Using 'do' keyword incorrectly
Using colon instead of braces
3. What will be the output of this Swift code?
var count = 3
while count > 0 {
print(count)
count -= 1
}
medium
A. 1 2 3
B. 3 2 1
C. 3 2 1 0
D. 0 1 2 3
Solution
Step 1: Analyze the loop condition and variable
Variable count starts at 3 and loop runs while count > 0.
Step 2: Trace each iteration
Prints 3, then count becomes 2; prints 2, count 1; prints 1, count 0; loop stops.
Final Answer:
3 2 1 -> Option B
Quick Check:
Loop prints count down from 3 to 1 [OK]
Hint: Count down from 3 to 1, stops before 0 [OK]
Common Mistakes:
Including 0 in output
Printing in ascending order
Not decreasing count inside loop
4. Identify the error in this Swift code:
var i = 1
while i < 5 {
print(i)
}
medium
A. Variable i should be a constant
B. Incorrect while syntax
C. Missing increment of i inside the loop
D. print statement is invalid
Solution
Step 1: Check loop structure and variable changes
The loop condition depends on i, but i never changes inside the loop.
Step 2: Understand effect of missing increment
Without increasing i, the condition i < 5 stays true forever, causing an infinite loop.
Final Answer:
Missing increment of i inside the loop -> Option C
Quick Check:
Loop variable must change to avoid infinite loop [OK]
Hint: Always update loop variable inside while loop [OK]
Common Mistakes:
Forgetting to increment or change loop variable
Thinking print causes error
Assuming syntax is wrong
5. How can you use a while loop to sum all even numbers from 2 to 10 inclusive in Swift?
hard
A. Use a variable starting at 2, add it to sum, increment by 2 until 10
B. Start at 1, add all numbers, then multiply by 2
C. Use a while loop with condition i <= 10, increment i by 2, add i
D. Use a while loop starting at 10, increment 2 each time, add to sum
Solution
Step 1: Plan loop to add even numbers from 2 to 10
Start with i = 2, add i to sum, then increase i by 2 to get next even number.
Step 2: Check options for correct approach
Use a variable starting at 2, add it to sum, increment by 2 until 10 correctly describes starting at 2, adding to sum, incrementing by 2 until 10.
Final Answer:
Use a variable starting at 2, add it to sum, increment by 2 until 10 -> Option A
Quick Check:
Sum evens by stepping 2 from 2 to 10 [OK]
Hint: Increment by 2 to sum even numbers quickly [OK]
Common Mistakes:
Incrementing by 2 without starting at 2
Starting from 10 and incrementing
Multiplying sum incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.