Loops
Swift has two loop forms: for-in, built around iterating a sequence (a range, an array, anything else that's iterable), and while, for repeating until a condition changes. There's no C-style three-part for loop in Swift at all.
for-in over a range
Swift main.swift
for i in 1...5 { print("Count: \(i)") }
Output
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
1...5 is a closed range that includes both ends. Swap it for 1..<5 (a half-open range) if you want to stop before 5 instead of at it.
for-in over an array
Swift main.swift
let fruits = ["apple", "banana", "cherry"] for fruit in fruits { print(fruit) }
Output
apple banana cherry
while
Swift main.swift
var stock = 100 while stock > 0 { stock -= 30 } print("Stock ended at \(stock)")
Output
Stock ended at -20
break and continue
Swift main.swift
for n in 1...10 { if n % 2 == 0 { continue } if n > 7 { break } print(n) }
Output
1 3 5 7
Note:
\(i) inside a string literal is string interpolation — it evaluates i and splices its text representation directly in, the same job template literals do in JavaScript with ${}. You'll see it constantly in idiomatic Swift instead of concatenating strings with +.