Swift Academy · Lesson

while and repeat-while Loops

Condition-based loops and guaranteeing at least one execution with repeat-while.

Lesson 2 of 412 steps

while and repeat-while Loops is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

When the number of iterations isn't known in advance, `while` and `repeat-while` loops are the right choice. Let's see when and how to use each.

The while Loop

A `while` loop evaluates its condition *before* each iteration: ```swift var count = 0 while count < 5 { print(count) count += 1 } // prints 0, 1, 2, 3, 4 ``` If the condition is false initially, the body never runs.

The repeat-while Loop

A `repeat-while` loop evaluates its condition *after* each iteration, guaranteeing at least one execution: ```swift var x = 10 repeat { print(x) x -= 3 } while x > 0 // prints 10, 7, 4, 1 ```

while vs repeat-while

Use `while` when you might not need to execute at all. Use `repeat-while` when you must execute at least once: ```swift // Reading input until valid — must try at least once: repeat { input = readLine() ?? "" } while input.isEmpty ```

Infinite Loops with while true

A `while true` loop runs forever until a `break` is hit — common in event loops: ```swift while true { let event = nextEvent() if event == .quit { break } handleEvent(event) } ``` Always ensure there is a reachable exit condition.

Countdown Example

```swift var countdown = 5 while countdown > 0 { print("\(countdown)...") countdown -= 1 } print("Liftoff!") ``` This is a classic while-loop pattern — decrement until the condition fails.

Avoiding Infinite Loops

Common infinite-loop mistakes: ```swift var i = 0 while i < 5 { print(i) // forgot i += 1 ← infinite loop! } ``` Always verify the loop variable is updated and the condition will eventually become false.

while with Optional Binding

Combine `while` with optional binding to process a sequence until nil: ```swift var items: [Int] = [1, 2, 3] while let value = items.first { print(value) items.removeFirst() } ``` This pattern is clean for consuming collections destructively.

Game Loop Example

```swift var lives = 3 var gameOver = false while !gameOver { let hit = Bool.random() if hit { lives -= 1 } if lives == 0 { gameOver = true } } print("Game Over") ``` Real game loops use this pattern to keep running until a terminal condition.

Nested while Loops

While loops can be nested — common in parsing or matrix algorithms: ```swift var row = 0 while row < 3 { var col = 0 while col < 3 { print("(\(row),\(col))", terminator: " ") col += 1 } print(); row += 1 } ```

Quick Check

Which loop guarantees its body runs **at least once**?

Recap

Key takeaways: • `while condition { }` — checks before each run; may never execute • `repeat { } while condition` — checks after; runs at least once • `while true { ... break }` — event-loop pattern • Always ensure the loop variable progresses toward termination • Combine `while` with optional binding for clean sequence consumption Next: iterating collections with for-in.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “while and repeat-while Loops” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “while and repeat-while Loops”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “while and repeat-while Loops”?

Condition-based loops and guaranteeing at least one execution with repeat-while. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “while and repeat-while Loops” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. for-in Loops with Ranges
  2. while and repeat-while Loops
  3. Iterating Collections with for-in
  4. break, continue and Labeled Statements
← Back to Swift Academy