Foundations
Loops
Loops repeat a block of code. A for loop is a direct way to walk through a
range of numbers.
Sum a range
loops.swift
let limit =
var total = 0
for value in 1...limit {
total = total + value
}
print("limit=\(limit)")
print("total=\(total)")
let limit =
var total = 0
for value in 1...limit {
total = total + value
}
print("limit=\(limit)")
print("total=\(total)")
let limit =
var total = 0
for value in 1...limit {
total = total + value
}
print("limit=\(limit)")
print("total=\(total)")
closed range
`1...limit` includes both endpoints.