Bird
Raised Fist0
Swiftprogramming~20 mins

For-in loop with ranges in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift For-in Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1: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: " ")
}
A1 2 3
B1 2
C0 1 2 3
D2 3 4
Attempts:
2 left
💡 Hint
The closed range operator '...' includes both the start and end values.
❓ Predict Output
intermediate
1: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: ",")
}
A0,1,2,
B0,1,2,3,4,
C1,2,3,4,
D0,1,2,3,
Attempts:
2 left
💡 Hint
The half-open range operator '..<' excludes the upper bound.
🔧 Debug
advanced
2: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")
    }
}
ASyntax error: Cannot assign to value: 'i' is a 'let' constant
BRuntime error: Index out of range
CSyntax error: Expected '==' in conditional
DNo error, prints 'Three' once
Attempts:
2 left
💡 Hint
Check the condition inside the if statement for correct comparison syntax.
🧠 Conceptual
advanced
1: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)
}
A10 times
B4 times
C5 times
D3 times
Attempts:
2 left
💡 Hint
The stride goes from 0 up to but not including 10, stepping by 3 each time.
❓ Predict Output
expert
2: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: " ")
    }
}
A1 2 2 4
B1 1 2 2
C2 4 1 2
D1 2 3 4
Attempts:
2 left
💡 Hint
Multiply each pair of i and j values in the nested loops.

Practice

(1/5)
1. What does the following Swift code print?
for i in 1...3 { print(i) }
easy
A. 1 2
B. 1 2 3
C. 0 1 2 3
D. 2 3 4

Solution

  1. Step 1: Understand the range operator

    The range 1...3 is a closed range including 1, 2, and 3.
  2. Step 2: Loop prints each number in the range

    The loop prints 1, then 2, then 3 on separate lines.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Closed range includes last number = 1 2 3 [OK]
Hint: Closed range includes last number, so count all numbers [OK]
Common Mistakes:
  • Thinking the last number is excluded
  • Starting from 0 instead of 1
  • Confusing closed and half-open ranges
2. Which of the following is the correct syntax for a for-in loop iterating from 0 to 4 (excluding 4) in Swift?
easy
A. for i in 0...4 { }
B. for i in 0 to 4 { }
C. for i in 0..<4 { }
D. for i = 0; i < 4; i++ { }

Solution

  1. Step 1: Identify the correct range syntax

    0..<4 is a half-open range including 0,1,2,3 but excluding 4.
  2. Step 2: Check Swift for-in loop syntax

    Swift uses 'for variable in range { }' syntax; 'for i in 0..<4 { }' is correct.
  3. Final Answer:

    for i in 0..<4 { } -> Option C
  4. Quick Check:

    Half-open range excludes last number = for i in 0..<4 { } [OK]
Hint: Use '..<' for half-open range excluding last number [OK]
Common Mistakes:
  • Using '...' when last number should be excluded
  • Using 'to' keyword which is invalid in Swift
  • Using C-style for loop syntax which is not valid in Swift
3. What is the output of this Swift code?
var sum = 0
for number in 1..<5 {
  sum += number
}
print(sum)
medium
A. 10
B. 15
C. 14
D. Error

Solution

  1. Step 1: Identify the range and loop values

    The range 1..<5 includes 1, 2, 3, 4 (excluding 5).
  2. Step 2: Calculate the sum of these numbers

    Sum = 1 + 2 + 3 + 4 = 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Sum of 1 to 4 = 10 [OK]
Hint: Half-open range excludes last number; sum 1+2+3+4 = 10 [OK]
Common Mistakes:
  • Including 5 in the sum
  • Adding numbers incorrectly
  • Confusing closed and half-open ranges
4. The following Swift code has an error. What is it?
for i in 1..< { print(i) }
medium
A. No error, code runs fine
B. Using wrong loop variable
C. Using closed range instead of half-open
D. Missing end value in range

Solution

  1. Step 1: Check the range syntax

    The range 1..< is incomplete; it misses the end value after '..<' operator.
  2. Step 2: Identify the error type

    This causes a syntax error because Swift expects a number after '..<' to define the range.
  3. Final Answer:

    Missing end value in range -> Option D
  4. Quick Check:

    Range must have start and end values [OK]
Hint: Range must have start and end values, '..<' alone is invalid [OK]
Common Mistakes:
  • Leaving out the end value in range
  • Confusing '..<' with '...' syntax
  • Assuming loop variable causes error
5. You want to print all even numbers from 2 to 10 inclusive using a for-in loop with a range. Which code correctly does this?
hard
A. for i in 2...10 where i % 2 == 0 { print(i) }
B. for i in 2..<10 { if i % 2 == 0 { print(i) } }
C. for i in 2...10 { if i % 2 == 1 { print(i) } }
D. for i in 2...9 where i % 2 == 0 { print(i) }

Solution

  1. Step 1: Understand the range and condition

    We want even numbers from 2 to 10 inclusive, so use closed range 2...10.
  2. Step 2: Use 'where' clause to filter even numbers

    The 'where i % 2 == 0' filters only even numbers in the loop.
  3. Final Answer:

    for i in 2...10 where i % 2 == 0 { print(i) } -> Option A
  4. Quick Check:

    Closed range + where filter = for i in 2...10 where i % 2 == 0 { print(i) } [OK]
Hint: Use closed range and 'where' to filter even numbers [OK]
Common Mistakes:
  • Using half-open range excluding 10
  • Printing odd numbers by mistake
  • Forgetting to filter even numbers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard