What a while loop does
A while loop repeats a block of code as long as a condition is true. Before every pass the condition is checked. If it is true, the body runs. If it is false, the loop ends and the program continues after it.
Go has no while keyword. It has one loop keyword, for, and a for followed by only a condition does the job:
for condition {
instruction1
instruction2
}
There is no initialization and no post statement here, just the condition and the body. Most Go programmers still call this a while loop.
An example, traced
count := 1
for count <= 5 {
fmt.Println("I made a mistake")
count++
}
fmt.Println("Finished!")
The program prints the message five times and then Finished!. Here is why:
- The variable
countis created with the value 1. This happens once, before the loop. - The condition
count <= 5is checked. It is true for 1, so the body runs: the message is printed andcountbecomes 2. - The same check and body repeat for 2, 3, 4 and 5. After the pass with 5,
countbecomes 6. - The check
6 <= 5is false, so the loop ends. The program continues with the line after the closing brace and printsFinished!.
The condition is checked before the body, so a loop whose condition is false at the start never runs at all. With count set to 7 first, nothing would be printed by the loop, and only Finished! would appear.
The three ingredients
A working while loop almost always has three parts, and each one is a place for a mistake:
- Initialization. A variable declared before the loop, such as
count := 1. - Condition. A boolean expression that uses the variable, such as
count <= 5. - Update. A statement in the body that moves the variable toward making the condition false, such as
count++.
If you forget the update, the condition stays true forever and the loop never ends. A later lesson covers this.
The exercise
The task is to print I promise to learn coding. five times, this time with a loop. Build it in four steps, which match the three ingredients plus the work the loop does:
- Declare
counterwith:=and the value 1. The short declaration works because Go infers the typeint. - Write a
forwith only the conditioncounter <= 5. Starting at 1 and including 5 gives five passes. - Inside the braces, print the sentence with
fmt.Println. - Still inside the braces, add 1 to
counter. Without this line the counter would stay at 1 and the loop would never end.
counter := 1
for counter <= 5 {
fmt.Println("I promise to learn coding.")
counter += 1
}
The line counter += 1 means the same as counter = counter + 1. In Go, counter++ also adds one, but it is a statement and not an expression, so you cannot write x = counter++.
Go details
- The condition must be a boolean. Go does not treat the number 1 or a non-empty value as true, so
for counter {does not compile whencounteris anint. - No parentheses are needed around the condition, and the opening brace has to be on the same line as
for. - A variable declared with
:=before the loop is still visible after it. In the example above,countis 6 once the loop has finished.
Key takeaways
- A while loop in Go is a
forwith only a condition. - The condition is checked before every pass, and the loop ends as soon as it is false.
- Declare the variable before the loop, test it in the condition, and change it inside the body.