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!