What a while loop does
A while loop repeats a block of code as long as a condition is true. Use it when you know the rule for stopping but not necessarily how many passes it takes.
while condition {
instruction1
instruction2
}
Swift needs no parentheses around the condition, but the braces are required, even for a single statement. The condition must be a Bool: Swift does not treat numbers as true or false.
An example, step by step
var count = 1
while count <= 5 {
print("I made a mistake")
count += 1
}
print("Finished!")
The output is the sentence five times, then Finished!. This is the sequence the computer follows:
- It creates
countwith the value 1. - It checks
count <= 5. Since 1 <= 5 is true, it runs the body: it prints the sentence and makescount2. - It checks the condition again, with 2, 3, 4 and 5 in turn. Each time the condition is true, so the body prints and increases
count. - After the fifth pass,
countis 6. The condition 6 <= 5 is false, so the loop ends. - The flow continues after the closing brace and prints
Finished!.
The condition is checked before every pass, including the first. If it is false from the start, the body never runs. A while loop is like an if statement that jumps back to the condition after each run.
The three parts of a counting loop
- Initialisation. The variable is created before the loop:
var count = 1. - Condition. The loop keeps going while it is true:
count <= 5. - Update. Something in the body moves the variable toward making the condition false:
count += 1.
If the update is missing, the condition never becomes false and the loop is infinite. Swift has no ++ operator, so you increase a variable with += 1. The counter must be declared with var, since a let constant can't be changed.
The exercise
The assignment is to print "I promise to learn coding." five times with a loop.
- Declare the counter. Write
var counter = 1. - Write the loop.
while counter <= 5 {runs for the values 1, 2, 3, 4 and 5, so five times. - Print inside the body with
print("I promise to learn coding."). - Update the counter with
counter += 1, still inside the braces.
var counter = 1
while counter <= 5 {
print("I promise to learn coding.")
counter += 1
}
When the condition is false at the start
Because the check comes first, a while loop can run zero times. If count started at 10 in the example above, 10 <= 5 would be false, the body would be skipped, and only Finished! would print. That is useful: code that handles a list of items does nothing when there are none.
Common mistakes
- Forgetting the update.
counterstays 1 and the loop never ends. - Off by one. With
counter < 5the loop runs for 1 to 4, which is four passes. Starting at 1, use<=to run five times. - Using
let. The compiler rejectscounter += 1on a constant. - Writing
counter++. The compiler reports that the operator does not exist and suggests+= 1. - Putting the update after the loop. It would run once, too late to matter.
Key takeaways
- A
whileloop checks its condition before every pass and stops when it is false. - A counting loop needs an initial value, a condition and an update.
- Use
varand+= 1in Swift.
