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.
- typed value
- Swift expression or function
- safe result
Explain the type, ownership, and runtime behavior of Loops (For-in, While)
Core syntax or pattern
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
- typed value
let 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.
- Swift expression or function
while 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.
- safe result
print("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.
- typed value
- Swift expression or function
- 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.
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.
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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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