Loops are used all the time in programming. You have options between while and for loops based on your needs.
A loop typically ends when a task is completed or a condition changes (unless you set yourself up with an infinite loop).
But there are times where you need to control the loop from inside the loop itself.
There are two special keywords whose job is to control the flow of a loop from within the loop itself.
In both cases, you simply put the keyword on its own line in the code.
Use the continue keyword to return immediately to the top of the loop for the next iteration without completing the rest of the code in the loop. This keeps the loop going overall, but you can skip certain sections if you need to.
Let's say your teacher is calling up each student to the front of the class to introduce themselves, read a poem, then take a bow. The teacher calls one student after another. But if one student is absent, the teacher just moves on to the next person instead of looking for the introduction and so on. This is essentially what a continue is doing.
Let's take an example of counting by 10
up to 100
. But we want to skip the number 60
.
Eh, why not?
The continue makes the loop jump up to the top and so it skips the console.log()
call.
Use break to exit out of a loop completely without finishing anything else in it. It doesn't run any more of the code in the block and it doesn't do any more iterations. The loop just stops and the program moves on to whatever else is next.
Let's use our classroom idea from above. Assume all the students are in class, so everyone is going to be called up to make their introduction, read their poem, and then take their bow. There's no escape this time! But then... uh oh, the fire alarm goes off! The teacher escorts your class to safety and you wait there. The fire alarm interrupted the loop, so you're finished with readings for the day.
We are going to count again. We're going to count by 10
and go up to 100
. But this time,
once we get to 60, someone in the room shouts and interrupts the count, so we stop.
The break puts a stop to everything. The loop counts up like it did before, starting a
0
and going up by 10
. But this time, the display stops at 50
. Once the count
is at 60
, the loop breaks and exits. It never gets to the console.log()
line to show the 60
.
If you've seen switch statements, then you know that the break keyword is important there too. A switch tests for numerous cases and for each one, generally, you want to stop the switch and exit out. You use the break command here too.
You can read more switch statements here.
There are times where you have a lot of situations to control for in your loop. You may want to skip over things in some situations and end the process in others. You can put both of these keywords to work for you.
Sticking with our counting to 100 example, let's still count to 100
, but let's count by
3
this time. We'll display the values, but only the odd ones. And if, along the way, we go higher than
the number 50
, then let's stop and walk away.
Try it out. Does it do what you think it will do?
0
.100
non-inclusive.
i
, will increase by 3
each time through the loop.
i += 3
i
, is an even number then continue the loop.
i % 2 === 0
tests for even numbers using modulo.i
, is greater than 50
, then break out of
the loop.
What do we get?
Notice, they count up by 3
, but there are no even numbers shown (like 6
, 18
,
etc.) and there's nothing above 50
.
For practice, let's combine the two classroom situations from above and make it into a loop using all that we know.
For added practice, we're going to make an array of students
and loop through that. This is more
likely how you'll use these statements.
For even more added practice, we're going to use a random number to determine if there's a fire drill or if someone is absent. You may need to run it a couple of times to see its effect.
That's a hefty bit of code, but see if you can follow it through. Run it a few times. Do you get a fire drill? Is anyone ever absent? Play with the values and the names. Have some fun.