Thuta Learning
Swift
IntermediateProgrammingBeginner

Loops (For-in, While)

SwiftLesson 1120 min

What you'll walk away with

  • Explain the type, ownership, and runtime behavior of Loops (For-in, While)
  • Test nil, error, boundary, and concurrency cases
  • Write safe and testable Swift

Explain it simply

Loops (For-in, While), in plain language

A loop runs a block of code over and over again. Whenever you need to go through each item in an array, count down, or filter a data list, a loop is pretty much non-negotiable.

Problem → Why → Solution

The problem — why this concept matters

If you don't update the condition inside a while loop so it eventually becomes false, you'll end up with an infinite loop.

A common mistake

Assuming one sample output proves nil, ownership, actor isolation, and every error path.

Core concept — understand how it works

For Loops (For-in, While), define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.

Mental model
  1. typed value
  2. Swift expression or function
  3. safe result

Explain the type, ownership, and runtime behavior of Loops (For-in, While)

Core syntax or pattern

swift
let users = ["Aung", "Nandar", "Sai"]

for user in users {
    print("Welcome, \(user)!")
}

var countdown = 3
while countdown > 0 {
    print(countdown)
    countdown -= 1
}

print("Go!")

For Loops (For-in, While), define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.

Read the code step by step

  1. typed valuelet users = ["Aung", "Nandar", "Sai"]

    A loop runs a block of code over and over again. Whenever you need to go through each item in an array, count down, or filter a data list, a loop is pretty much non-negotiable.

  2. Swift expression or functionwhile countdown > 0 {

    For Loops (For-in, While), define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.

  3. safe resultprint("Go!")

    Run and test an original Loops (For-in, While) example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Execution flow
  1. typed value
  2. Swift expression or function
  3. safe result

Success

Explain the type, ownership, and runtime behavior of Loops (For-in, While)

Failure

If you don't update the condition inside a while loop so it eventually becomes false, you'll end up with an infinite loop.

A loop runs a block of code over and over again. Whenever you need to go through each item in an array, count down, or filter a data list, a loop is pretty much non-negotiable.

swift
let users = ["Aung", "Nandar", "Sai"]

for user in users {
    print("Welcome, \(user)!")
}

var countdown = 3
while countdown > 0 {
    print(countdown)
    countdown -= 1
}

print("Go!")

for-in runs once for each item in a collection. while runs as long as the condition stays true. Just remember — if you don't decrease the countdown value, the loop will never end.

You should see
Welcome, Aung! Welcome, Nandar! Welcome, Sai! 3 2 1 Go!

Real-world usage

How to apply it in a real project

Run and test an original Loops (For-in, While) example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Mini challenge

Build it yourself

Run and test an original Loops (For-in, While) example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
swift
let users = ["Aung", "Nandar", "Sai"]

for user in users {
    print("Welcome, \(user)!")
}

var countdown = 3
while countdown > 0 {
    print(countdown)
    countdown -= 1
}

print("Go!")

Expected: Explain the type, ownership, and runtime behavior of Loops (For-in, While)

Show hint

Run and test an original Loops (For-in, While) example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Quick quiz 1 / 3

What best describes the core idea of Loops (For-in, While)?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed value → Swift expression or function → safe result
  • For Loops (For-in, While), define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.
  • If you don't update the condition inside a while loop so it eventually becomes false, you'll end up with an infinite loop.
  • Run and test an original Loops (For-in, While) example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

The Swift Programming Language — Swift.org

Easy traps

  • If you don't update the condition inside a while loop so it eventually becomes false, you'll end up with an infinite loop.
  • Assuming one sample output proves nil, ownership, actor isolation, and every error path.
  • Bypassing compiler safety with force unwraps or unchecked shared mutable state.

Hands-on Exercise

Run and test an original Loops (For-in, While) example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

You'll know it worked when: Welcome, Aung! Welcome, Nandar! Welcome, Sai! 3 2 1 Go!

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Loops (For-in, While) | Thuta Learning