Thuta Learning
IntermediateProgrammingbeginner

Loops (For-in, While)

Relax. We'll talk through this in plain words — no textbook voice.

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!

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.
Loops (For-in, While) | Thuta Learning