How does a programmer get more direct control over a loop? With the break
and continue
keywords!
The break
keyword allows the programmer to stop the loop at the current iteration.
For example, if using a loop to search for a specific pet, the loop can be made to break
and stop when that animal has been found:
animals := []string{"Cat", "Dog", "Fish", "Turtle"} for index := 0; index < len(animals); index++ { if animals[index] == "Dog" { fmt.Println("Found the perfect animal!") break // Stop searching the array } }
The continue
keyword works similarly, allowing the loop to skip to the next iteration.
For example, there may be an array filled with jellybeans. To not eat the disgusting green jellybeans, a continue
statement can be used to skip them:
jellybeans := []string{"green", "blue", "yellow", "red", "green", "yellow", "red"} for index := 0; index < len(jellybeans); index++ { if jellybeans[index] == "green" { continue } fmt.Println("You ate the", jellybeans[index], "jellybean!") }
However, using continue
and break
statements tend to cause confusion over how a loop will behave. A break
statement changes when a loop will end. While a continue
statement changes what will happen in each loop.
While these keywords may not always be the best choice, they work really well for the use case covered in the next exercise, iterating through arrays and maps!
Instructions
It is great to count numbers, but there are so many of them! Let’s skip the number 8
.
Inside the existing loop, when count
is 8
, use the continue
keyword to skip to the next iteration.
Why count all the way to 20
? Let’s stop at 15
.
Similar to the last checkpoint, when the count
is equal to 15
, use the break
keyword to stop the loop.