Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
1
00:00:00.000 --> 00:00:04.000
With all the
2
00:00:04.000 --> 00:00:08.000
loops you have learned about in this section, there are two keywords,
3
00:00:08.000 --> 00:00:12.000
break and continue that can change how the loop behaves. In this demo I'm going to
4
00:00:12.000 --> 00:00:16.000
use a while loop. But what you're going to learn applies to all loops
5
00:00:16.000 --> 00:00:20.000
we have learned in this section. So let's start by declaring a variable
6
00:00:20.000 --> 00:00:24.000
called i and initialize it to 0. Now we put this
7
00:00:24.000 --> 00:00:28.000
in a while loop, as long as while is less than or equal to
8
00:00:28.000 --> 00:00:32.000
10, we're going to display i on the console
9
00:00:32.000 --> 00:00:36.000
and then increment it. Save the changes, so
10
00:00:36.000 --> 00:00:40.000
this gives us numbers 0-10. Now sometimes you want to jump
11
00:00:40.000 --> 00:00:44.000
out of a loop for some reason that may happen at run time.
12
00:00:44.000 --> 00:00:48.000
For example, here we can have an if condition
13
00:00:48.000 --> 00:00:52.000
an if statement, with a condition like this.
14
00:00:52.000 --> 00:00:56.000
If i equals 5, we want to jump out of this loop.
15
00:00:56.000 --> 00:01:00.000
That's where we use the break keyword. Now when we save
16
00:01:00.000 --> 00:01:04.000
the changes, see what happens we get the numbers 0
17
00:01:04.000 --> 00:01:08.000
to 4. So at the end of the 5th iteration here we
18
00:01:08.000 --> 00:01:12.000
increment i, now i is 5, so we break out of the loop.
19
00:01:12.000 --> 00:01:16.000
Now let me comment this out and look at the continue keyword.
20
00:01:16.000 --> 00:01:20.000
So I'm going to write another if statement, I want to see
21
00:01:20.000 --> 00:01:24.000
if i is an even number or not. So i,
22
00:01:24.000 --> 00:01:28.000
module is 2, equals 0. If that's the case,
23
00:01:28.000 --> 00:01:32.000
I want to increment i and then continue.
24
00:01:32.000 --> 00:01:36.000
Let's see what happens when we run this code. Save the changes,
25
00:01:36.000 --> 00:01:40.000
we only get the odd numbers, why is that? Alright
26
00:01:40.000 --> 00:01:44.000
let's take a look at an example. So when i becomes 2,
27
00:01:44.000 --> 00:01:48.000
it's an even number, at this point increment i,
28
00:01:48.000 --> 00:01:52.000
i will be 3. Now when the JavaScript engine sees the
29
00:01:52.000 --> 00:01:56.000
continue keyword. It will jump to the beginning of the loop.
30
00:01:56.000 --> 00:02:00.000
And continues it's execution in the next iteration.
31
00:02:00.000 --> 00:02:04.000
At this point i is 3, so this if statement is not executed,
32
00:02:04.000 --> 00:02:08.000
that's why we see i on the console. Now,
33
00:02:08.000 --> 00:02:12.000
in my personal experience, continue is not something you will use that often,
34
00:02:12.000 --> 00:02:16.000
it's one of those old legacy things in JavaScript, I'm only explaining
35
00:02:16.000 --> 00:02:20.000
it here in case you see it in projects you're working on. It's not some
36
00:02:20.000 --> 00:02:24.000
thing that I recommend you to use, it's an ugly way of writing code.
37
00:02:24.000 --> 00:02:28.000
So just to recap with the correct keyword, you jump out of a
38
00:02:28.000 --> 00:02:32.000
loop, and with the continue keyword, we jump to the next iteration.
3405
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.