Ranges and loops in Swift
1...3 includes the end, 0..<3 stops short, stride steps.
// Closed ranges include the end; half-open ranges stop short.
for lap in 1...3 {
print("Lap \(lap) of 3")
}
for i in 0..<3 {
print("Start sequence: \(3 - i)")
}
// stride covers steps; while covers everything else.
for depthM in stride(from: 10, through: 2, by: -4) {
print("Sounding: \(depthM) m")
}
var hullCheck = 3
while hullCheck > 0 {
hullCheck -= 1
}
print("Hull clear")
How it works
for lap in 1...3iterates the closed range.0..<3is half-open — the collection-index shape.stride(from:through:by:)counts down by fours;whileguards.
Keywords and builtins used here
forinprintstridevarwhile
The run, in numbers
- Lines
- 18
- Characters to type
- 380
- Tokens
- 92
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 79 seconds.
Step 3 of 3 in Control flow, step 6 of 27 in Language basics.