Challenge - 5 Problems
Swift For-in Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of a for-in loop with a closed range
What is the output of this Swift code?
for i in 1...3 {
print(i, terminator: " ")
}Swift
for i in 1...3 { print(i, terminator: " ") }
Attempts:
2 left
💡 Hint
The closed range operator '...' includes both the start and end values.
✗ Incorrect
The for-in loop iterates over the range 1 to 3 inclusive, printing each number with a space.
❓ Predict Output
intermediate1:30remaining
Output of a for-in loop with a half-open range
What does this Swift code print?
for i in 0..<4 {
print(i, terminator: ",")
}Swift
for i in 0..<4 { print(i, terminator: ",") }
Attempts:
2 left
💡 Hint
The half-open range operator '..<' excludes the upper bound.
✗ Incorrect
The loop runs from 0 up to but not including 4, printing each number followed by a comma.
🔧 Debug
advanced2:00remaining
Identify the error in the for-in loop with range
What error does this Swift code produce?
for i in 1...5 {
if i = 3 {
print("Three")
}
}Swift
for i in 1...5 { if i == 3 { print("Three") } }
Attempts:
2 left
💡 Hint
Check the condition inside the if statement for correct comparison syntax.
✗ Incorrect
The code uses '=' which is assignment, but in conditions you must use '==' for comparison.
🧠 Conceptual
advanced1:30remaining
Number of iterations in a for-in loop with stride
How many times does this Swift loop run?
for i in stride(from: 0, to: 10, by: 3) {
print(i)
}Swift
for i in stride(from: 0, to: 10, by: 3) { print(i) }
Attempts:
2 left
💡 Hint
The stride goes from 0 up to but not including 10, stepping by 3 each time.
✗ Incorrect
The values are 0, 3, 6, 9 — four iterations before reaching 10.
❓ Predict Output
expert2:00remaining
Output of nested for-in loops with ranges
What is the output of this Swift code?
for i in 1...2 {
for j in 1...2 {
print(i * j, terminator: " ")
}
}Swift
for i in 1...2 { for j in 1...2 { print(i * j, terminator: " ") } }
Attempts:
2 left
💡 Hint
Multiply each pair of i and j values in the nested loops.
✗ Incorrect
The outer loop runs i=1,2; inner loop runs j=1,2; products are 1*1=1, 1*2=2, 2*1=2, 2*2=4.