Bird
Raised Fist0
Swiftprogramming~10 mins

Range operators (... and ..<) in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Range operators (... and ..<)
Start
↓
Define range using ... or ..<
↓
Check range type
↓
... inclusive
↓
Generate numbers including end
↓
Use range in loop or array
↓
End
This flow shows how Swift uses ... for inclusive ranges and ..< for exclusive ranges to generate sequences of numbers.
Execution Sample
Swift
for i in 1...3 {
    print(i)
}

for j in 1..<3 {
    print(j)
}
This code prints numbers 1 to 3 inclusive with ... and 1 to 2 with ..<.
Execution Table
StepVariableRange TypeValueActionOutput
1i1...3 (inclusive)1Print i1
2i1...3 (inclusive)2Print i2
3i1...3 (inclusive)3Print i3
4i1...3 (inclusive)4Check condition fails
5j1..<3 (exclusive)1Print j1
6j1..<3 (exclusive)2Print j2
7j1..<3 (exclusive)3Check condition fails
💡 Loops stop when the variable reaches the end of the range (inclusive for ..., exclusive for ..<).
Variable Tracker
VariableStartAfter 1After 2After 3Final
i-123Loop ends after 3
j-12-Loop ends before 3
Key Moments - 2 Insights
Why does the loop with 1...3 include the number 3 but the loop with 1..<3 does not?
The execution_table shows that the ... operator includes the end value (3), so the loop prints 3. The ..< operator excludes the end value, so the loop stops before reaching 3.
What happens if we try to use ..< with the same start and end number, like 3..<3?
The loop will not run because the start is equal to the end and ..< excludes the end, so the condition is false immediately, as seen in the exit conditions in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at step 3?
A3
B2
C4
D1
💡 Hint
Check the row where Step is 3 and Variable is i in the execution_table.
At which step does the loop with j in 1..<3 stop running?
AStep 6
BStep 5
CStep 7
DStep 4
💡 Hint
Look for the step where the condition fails for variable j in the execution_table.
If we change the range 1..<3 to 1...3 for variable j, how would the output change?
AIt would exclude the number 2
BIt would include the number 3 in the output
CIt would print only number 1
DIt would not print anything
💡 Hint
Compare the outputs for i and j in the execution_table to see the difference between ... and ..<.
Concept Snapshot
Swift range operators:
- ... is inclusive (includes end value)
- ..< is exclusive (excludes end value)
Use in loops to generate sequences
Example: for i in 1...3 prints 1,2,3
for j in 1..<3 prints 1,2
Full Transcript
This lesson shows how Swift uses two range operators: ... and ..<. The ... operator creates a range that includes the last number, so a loop using 1...3 will print 1, 2, and 3. The ..< operator creates a range that excludes the last number, so a loop using 1..<3 will print 1 and 2 only. The execution table traces each step of the loops, showing variable values and when loops stop. Key points include understanding why the inclusive range includes the end number and the exclusive range does not. The visual quiz tests understanding of these differences by asking about variable values at specific steps and the effect of changing range operators.

Practice

(1/5)
1. What does the Swift range operator 1...5 represent?
easy
A. A range including numbers 1, 2, 3, 4, and 5
B. A range including numbers 1, 2, 3, and 4 only
C. A range excluding number 1 but including 5
D. A range excluding both 1 and 5

Solution

  1. Step 1: Understand the closed range operator

    The ... operator creates a range that includes both the start and end values.
  2. Step 2: Apply to the given range

    For 1...5, it includes 1, 2, 3, 4, and 5.
  3. Final Answer:

    A range including numbers 1, 2, 3, 4, and 5 -> Option A
  4. Quick Check:

    1...5 includes 5 [OK]
Hint: Remember: ... includes end number, ..< excludes it [OK]
Common Mistakes:
  • Thinking ... excludes the last number
  • Confusing ... with ..<
  • Assuming ..< includes the end number
2. Which of the following is the correct syntax to create a half-open range from 0 to 9 in Swift?
easy
A. 0...9
B. 0..<10
C. 0..9
D. 0...10

Solution

  1. Step 1: Identify half-open range syntax

    The half-open range operator in Swift is ..<, which includes the start but excludes the end.
  2. Step 2: Apply to the range 0 to 9

    To include numbers 0 through 9, use 0..<10 because 10 is excluded.
  3. Final Answer:

    0..<10 -> Option B
  4. Quick Check:

    ..< excludes end [OK]
Hint: Use ..< to exclude the last number in range [OK]
Common Mistakes:
  • Using ... when half-open range is needed
  • Writing 0..9 which is invalid syntax
  • Confusing end number inclusion
3. What will be the output of the following Swift code?
for i in 3..<6 {
    print(i, terminator: ",")
}
medium
A. 3,4,5,6,
B. 4,5,6,
C. 3,4,5,
D. 3,4,5,6

Solution

  1. Step 1: Understand the range used in the loop

    The range 3..<6 includes 3, 4, and 5 but excludes 6.
  2. Step 2: Trace the loop output

    The loop prints 3, then 4, then 5, each followed by a comma.
  3. Final Answer:

    3,4,5, -> Option C
  4. Quick Check:

    ..<6 excludes 6 [OK]
Hint: ..< excludes end; loop stops before 6 [OK]
Common Mistakes:
  • Including 6 in output
  • Missing commas in output
  • Confusing terminator usage
4. The following Swift code has an error. What is the problem?
let range = 1..5
for num in range {
    print(num)
}
medium
A. The range operator .. is invalid in Swift
B. The loop variable num is not declared
C. The print statement syntax is incorrect
D. The range should start from 0

Solution

  1. Step 1: Check the range operator used

    The code uses 1..5, which is not a valid range operator in Swift.
  2. Step 2: Identify correct range operators

    Swift uses ... for closed ranges and ..< for half-open ranges, but not ...
  3. Final Answer:

    The range operator .. is invalid in Swift -> Option A
  4. Quick Check:

    Invalid operator .. causes error [OK]
Hint: Use ... or ..<, never .. alone [OK]
Common Mistakes:
  • Using .. instead of ... or ..<
  • Assuming .. is a valid operator
  • Ignoring compiler error messages
5. You want to create a Swift array containing numbers from 5 to 15, excluding 15, and then print only the even numbers. Which code snippet correctly uses range operators and filtering?
hard
A. let numbers = Array(5...15).filter { $0 % 2 == 0 } print(numbers)
B. let numbers = Array(5..15).filter { $0 % 2 == 0 } print(numbers)
C. let numbers = Array(5..<14).filter { $0 % 2 == 0 } print(numbers)
D. let numbers = Array(5..<15).filter { $0 % 2 == 0 } print(numbers)

Solution

  1. Step 1: Choose correct range excluding 15

    To exclude 15, use the half-open range 5..<15, which includes 5 up to 14.
  2. Step 2: Filter even numbers

    The filter condition { $0 % 2 == 0 } selects even numbers from the array.
  3. Step 3: Verify other options

    let numbers = Array(5...15).filter { $0 % 2 == 0 } print(numbers) includes 15, let numbers = Array(5..15).filter { $0 % 2 == 0 } print(numbers) uses invalid operator .., and let numbers = Array(5..<14).filter { $0 % 2 == 0 } print(numbers) uses 5..<14 which goes only up to 13, missing the even number 14.
  4. Final Answer:

    let numbers = Array(5..<15).filter { $0 % 2 == 0 } print(numbers) -> Option D
  5. Quick Check:

    ..< excludes end, filter selects even [OK]
Hint: Use ..< to exclude end, then filter even numbers [OK]
Common Mistakes:
  • Using ... to include 15 mistakenly
  • Using invalid .. operator
  • Not filtering correctly for 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