100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Loops in Swift (for-in, while, repeat-while)

Understand Swift's three loop types — for-in, while, and repeat-while — since the C-style for loop was removed.

Control FlowBeginner7 min readJul 8, 2026
Analogies

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

swift
for item in sequence {
    // use item
}

while condition {
    // runs while condition is true
}

repeat {
    // runs at least once
} while condition

Explanation

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

swift
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 < 3

Output

swift
for-in: 1
for-in: 2
for-in: 3
while: 0
while: 1
while: 2
repeat-while: 0
repeat-while: 1
repeat-while: 2

Key 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

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#LoopsInSwiftForInWhileRepeatWhile#Loops#While#Repeat#Syntax#StudyNotes#SkillVeris