break and continue
There are going to be times when you need to skip a single loop or stop a loop from running altogether. It’s possible to do this with variables and if
statements, but there is an easier way.
The continue
keyword stops the execution of the current loop and starts a new loop. The post
loop logic runs, and the loop condition
statement gets evaluated.
The break
keyword also stops the execution of the current loop and stops any new loops from running.
Use continue
when you want to skip a single item in a collection; for instance, perhaps it’s okay if one of the items in a collection is invalid, but the rest may be okay to process. Use break
when you need to stop processing when there are any errors in the data and there’s no value in processing the rest of the collection.
Here, we have an example that generates a random number between 0
and 8
. The loop skips on a number divisible by 3
and stops on a number divisible by 2
. It also prints...