Introduction
Swift provides three loop constructs for repeating code: for-in, while, and repeat-while. The for-in loop iterates over sequences such as ranges, arrays, and dictionaries. The while loop checks its condition before each iteration, running zero or more times. The repeat-while loop is Swift's equivalent of the classic do-while loop — it checks its condition after each iteration, guaranteeing the body runs at least once. Notably, Swift removed the traditional C-style for (initialization; condition; increment) loop back in Swift 3, favoring the more expressive for-in loop combined with ranges and the stride functions instead.
Cricket analogy: A bowler's over is a for-in loop through six fixed deliveries, a captain's field review is a while loop that checks the pitch condition before each change, and repeat-while is the umpire's rain inspection, which always happens once before deciding whether to inspect again — Swift dropped the old ball-by-ball tally counter back in Swift 3.
Syntax
for item in sequence {
// use item
}
while condition {
// runs while condition is true
}
repeat {
// runs at least once
} while conditionExplanation
In a for-in loop, sequence can be a range (1...5), an array, a dictionary, a string's characters, or any type conforming to the Sequence protocol. If you don't need the loop variable, you can use an underscore, like for _ in 1...3. The while loop evaluates condition before running the body each time, so the body may never execute if the condition starts false. The repeat-while loop evaluates the body first and checks condition afterward, guaranteeing at least one execution — this is directly analogous to do-while in C or Java, just with different keywords.
Cricket analogy: for _ in 1...3 is running three unnamed practice deliveries without tracking the count, while resets a batsman's stance only while the bowler's run-up condition stays true and may leave him standing still, and repeat-while mirrors a no-ball review that always happens once before the umpire re-checks — the do-while of the crease.
Example
for number in 1...3 {
print("for-in: \(number)")
}
var count = 0
while count < 3 {
print("while: \(count)")
count += 1
}
var attempts = 0
repeat {
print("repeat-while: \(attempts)")
attempts += 1
} while attempts < 3Output
for-in: 1
for-in: 2
for-in: 3
while: 0
while: 1
while: 2
repeat-while: 0
repeat-while: 1
repeat-while: 2Key Takeaways
- Swift has three loop types: for-in, while, and repeat-while.
- The traditional C-style for (init; condition; increment) loop was removed in Swift 3.
- for-in iterates over ranges, arrays, dictionaries, and any Sequence-conforming type.
- while checks its condition before each iteration, so it may run zero times.
- repeat-while checks its condition after the body, so it always runs at least once.
Practice what you learned
1. Which traditional loop construct was removed from Swift starting with Swift 3?
2. Which loop is guaranteed to execute its body at least once?
3. What can a for-in loop iterate over in Swift?
4. When does a while loop check its condition?
Was this page helpful?
You May Also Like
Control Transfer Statements in Swift
Master continue, break, fallthrough, return, and labeled statements for precise control over loops and switches.
Arrays in Swift
Learn how Swift arrays store ordered, mutable collections of values and how to manipulate them safely.
Collection Operations in Swift (map, filter, reduce)
Master Swift's functional-style collection operations — map, filter, reduce, compactMap, and sorted.
if-else Statements in Swift
Learn how Swift's if-else statement branches execution using strictly typed Bool conditions and mandatory braces.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics