Control Flow
While Loop
A while loop repeats while its condition remains true.
Repeat while true
while_loop.swift
let limit =
var value = 1
var total = 0
while value <= limit {
total = total + value
value = value + 1
}
print("limit=\(limit)")
print("total=\(total)")
let limit =
var value = 1
var total = 0
while value <= limit {
total = total + value
value = value + 1
}
print("limit=\(limit)")
print("total=\(total)")
let limit =
var value = 1
var total = 0
while value <= limit {
total = total + value
value = value + 1
}
print("limit=\(limit)")
print("total=\(total)")
while
Use a `while` loop when the number of repetitions is controlled by changing state rather than by directly iterating over a collection.