Introduction
A for-in loop with ranges helps you repeat actions a set number of times easily.
A for-in loop with ranges helps you repeat actions a set number of times easily.
for number in start...end { // code to repeat }
The three dots (...) create a closed range including both start and end.
You can also use two dots (..<) for a half-open range that excludes the end.
for i in 1...5 { print(i) }
for i in 0..<3 { print(i) }
for i in 5...5 { print(i) }
This program prints a message for each number from 1 to 5.
for number in 1...5 { print("Number is \(number)") }
Ranges are very useful to control how many times a loop runs.
Using a half-open range (..<) is common when working with arrays to avoid going out of bounds.
For-in loops with ranges repeat code for each number in the range.
Closed ranges (start...end) include the last number.
Half-open ranges (start..
for i in 1...3 { print(i) }var sum = 0
for number in 1..<5 {
sum += number
}
print(sum)for i in 1..< { print(i) }Pass the test and this pattern moves unknown