What if you could tell your code to count for you instead of writing every step yourself?
Why For-in loop with ranges in Swift? - Purpose & Use Cases
Imagine you want to count from 1 to 10 and print each number. Doing this by writing each number one by one is like writing a long list by hand.
Writing each step manually takes a lot of time and is easy to mess up. If you want to change the range, you must rewrite everything, which is slow and boring.
Using a for-in loop with ranges lets you tell the computer to count for you automatically. You just say the start and end numbers, and the loop does the rest.
print(1) print(2) print(3) print(4) print(5)
for number in 1...5 { print(number) }
This lets you repeat tasks easily and change the number of repetitions quickly without extra work.
Counting days in a calendar app to show each day number automatically instead of typing each day manually.
Manually repeating code is slow and error-prone.
For-in loops with ranges automate counting and repeating tasks.
They make code shorter, easier to read, and flexible.
Practice
for i in 1...3 { print(i) }Solution
Step 1: Understand the range operator
The range 1...3 is a closed range including 1, 2, and 3.Step 2: Loop prints each number in the range
The loop prints 1, then 2, then 3 on separate lines.Final Answer:
1 2 3 -> Option BQuick Check:
Closed range includes last number = 1 2 3 [OK]
- Thinking the last number is excluded
- Starting from 0 instead of 1
- Confusing closed and half-open ranges
Solution
Step 1: Identify the correct range syntax
0..<4 is a half-open range including 0,1,2,3 but excluding 4.Step 2: Check Swift for-in loop syntax
Swift uses 'for variable in range { }' syntax; 'for i in 0..<4 { }' is correct.Final Answer:
for i in 0..<4 { } -> Option CQuick Check:
Half-open range excludes last number = for i in 0..<4 { } [OK]
- 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
var sum = 0
for number in 1..<5 {
sum += number
}
print(sum)Solution
Step 1: Identify the range and loop values
The range 1..<5 includes 1, 2, 3, 4 (excluding 5).Step 2: Calculate the sum of these numbers
Sum = 1 + 2 + 3 + 4 = 10.Final Answer:
10 -> Option AQuick Check:
Sum of 1 to 4 = 10 [OK]
- Including 5 in the sum
- Adding numbers incorrectly
- Confusing closed and half-open ranges
for i in 1..< { print(i) }Solution
Step 1: Check the range syntax
The range 1..< is incomplete; it misses the end value after '..<' operator.Step 2: Identify the error type
This causes a syntax error because Swift expects a number after '..<' to define the range.Final Answer:
Missing end value in range -> Option DQuick Check:
Range must have start and end values [OK]
- Leaving out the end value in range
- Confusing '..<' with '...' syntax
- Assuming loop variable causes error
Solution
Step 1: Understand the range and condition
We want even numbers from 2 to 10 inclusive, so use closed range 2...10.Step 2: Use 'where' clause to filter even numbers
The 'where i % 2 == 0' filters only even numbers in the loop.Final Answer:
for i in 2...10 where i % 2 == 0 { print(i) } -> Option AQuick Check:
Closed range + where filter = for i in 2...10 where i % 2 == 0 { print(i) } [OK]
- Using half-open range excluding 10
- Printing odd numbers by mistake
- Forgetting to filter even numbers
Ready to prove it?
Pass the test and this pattern moves unknown
