An if
statement allows us to check a specific condition once. But what if that condition needs to be checked a certain number of times?
Definite loops can be used to repeat code a fixed number of times.
While an if
statement only has a conditional expression (like number < 5
), a definite loop has two additional components:
- An initial statement, which defines the starting value for a loop variable.
Ex:number := 0
- A post statement, which defines what happens at the end of each loop iteration.
Ex:number++
So while an if
statement that prints one number less than 5
may look like this:
number := 3 if number < 5 { fmt.Println(number) } // Output: 3
A for
loop that prints the numbers 0
, 1
, 2
, 3
, and 4
would look like this:
for number := 0; number < 5; number++ { fmt.Print(number, " ") } // Output: 0 1 2 3 4
While the if
statement prints a number once, the for
loop uses a similar syntax to print a number five times.
Let’s dive deeper into each component of that definite loop:
The initial statement,
number := 0
, creates a new variable to be used within thefor
loop code block.The conditional expression,
number < 5
, will stop the loop whennumber
reaches the target value of5
.The post statement,
number++
, increases the value of thenumber
variable by1
each time the code block completes.
In this example, the amount of times that the number
needed to be printed was known beforehand. But what do we do when we don’t know how many times a loop needs to run?
Find out more in the next exercise!
Instructions
You are hosting a party and trying to count the number of guests. The last host left a loop to do this, but there is a bug in the code!
Change the initial statement of the loop, count := 8
, so that the count begins at 1
instead of 8
.
Disaster has struck! Eight people cannot make it to the party!
Change the conditional expression of the loop, count <= 20
, so that the loop only counts up to 12
.
With the amount of guests set, count off the odd numbered guests to make teams for a game!
Change the post statement of the loop, count++
, so that count = count + 2
after the completion of each loop.