Break and Continue:
- Break:
For
loop withbreak
:For... in
loop withbreak
:For... of
loop withbreak
:While
loop withbreak
:Do... while
loop withbreak
:- Continue:
For
loop usingcontinue
:For... in
loop withcontinue
:For... of
loop withcontinue
:While
loop withcontinue
:Do... while
loop withcontinue
:
Iterating through a loop is useful, but what if we need to stop iterating at a certain point?
To do this, we use the break;
statement. Note in the code block below where the statement is in relation to
what we want the code to do. Our alert
etc. needs to be outside of the break
statement:
Let's return to our Dogs d'Amour albums [array]:
let dogsAlbums = ["(Un)authorised Bootleg Album", "Dynamite Jet Saloon", "Errol Flynn", "Straight",
"More Unchartered Heights Of Disgrace"];
We want to break
the code block at index #3 - in other words, we want indices 0, 1 and 2, but not index #3:
Note that we are looking to stop at an index #, not at a specific enry in the [array]
. In other words, we want to know what
the entry is at the given index #. If we replace the i === 3
with the name of one of the albums, the loop will not break, but will continue until the .length
is at its end:
Again, let's break
at index #3:
Since the second and third conditions (i<[array].length; i++
) are implied, we do not need these in the loop:
Note that i === 3
will not work in this situation, as the number 3 would be treated as a
"string"
if it were. We are not looking for an is identical to
.
Again, note that with the for... in
loop, the values of the [array]
could be anything. We
are looking for the index # (that is, does it entry #3 exist in the [array]
?) rather than its value.
Again, let's break
at index #3:
Again, the second and third conditions of the loop are implied. But with the for... of
loop,
we can name i
whatever we want. I have named it "album".
With the for... of
loop, we are looking for the "string"
value at a certain index #, not the index # itself,
and because of this we need to look for an is identical to
(===
):
Note that with while
loops, we need to declare the variable i
. For
loops self declare their starting points, whereas while
loops are conditional loops that function
until a condition is either met or not (depending on the code to be executed).
Again, let's break
at index #3:
We are looking for if index #3 exists. Not its value. Thus we do not need an is identical to
(===
):
Note that, again, with do... while
loops, we need to declare the variable i
. We also need to tell the code
what to alert/log/print BEFORE we run the loop (otherwise, we get an infinite loop). Thus, here, we tell the code to:
Alert the result
Increment
before we tell the loop where to break.
Again, let's break
at index #3:
Again, we are looking for if index #3 exists. Not its value. Thus we do not need an is identical to
(===
):
Break loops are great if we want to stop a block of code from looping. But what if we want to skip certain item
in an [array]
and then continue? This is where the continue
keyword comes in useful: we can skip even
numbers, odd numbers, numbers that are divisible by x... and of course, we can skip "string"
values in [arrays]
using this keyword.
We can see that the code is exactly the same, save for the keyword continue
.
numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Let's try the same with our Dogs d'Amour albums [array]
:
Note how we have had to slightly change the ordering of the code for the loop to work with continue.
The same is true for the do... while
loop: