0
0
Swiftprogramming~10 mins

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

Choose your learning style9 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.