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
Alright now let's
2
00:00:04.000 --> 00:00:08.000
take a look at the third kind of loop we have in JavaScript.
3
00:00:08.000 --> 00:00:12.000
That is a do-while loop. Do-while loops
4
00:00:12.000 --> 00:00:16.000
are very similar to y loops, but they're slightly different. It's
5
00:00:16.000 --> 00:00:20.000
easier to show you in code. So I'm going to rewrite this logic
6
00:00:20.000 --> 00:00:24.000
using a do-while loop. Just like the y loops, we have to declare
7
00:00:24.000 --> 00:00:28.000
our loop variable externally, so we declare i and
8
00:00:28.000 --> 00:00:32.000
initialize it to 0. Okay, now if we save the changes,
9
00:00:32.000 --> 00:00:36.000
we're going to get an error identifier has already been declared.
10
00:00:36.000 --> 00:00:40.000
Because I have declared i here, so I can not redeclare
11
00:00:40.000 --> 00:00:44.000
it here. This is different than the i that we have in the for loop,
12
00:00:44.000 --> 00:00:48.000
again, this is all about scoping, and I'm going to talk about that later in the course.
13
00:00:48.000 --> 00:00:52.000
So, to make this work I'm going to temporarily
14
00:00:52.000 --> 00:00:56.000
comment out these few lines, and rewrite this
15
00:00:56.000 --> 00:01:00.000
logic using a do while loop. So we add the do statement
16
00:01:00.000 --> 00:01:04.000
do statement here, then a code block, in this block we should
17
00:01:04.000 --> 00:01:08.000
have our statements. So I'm going to borrow them
18
00:01:08.000 --> 00:01:12.000
from our while loop, copy these, paste them here,
19
00:01:12.000 --> 00:01:16.000
and then finally at the end
20
00:01:16.000 --> 00:01:20.000
of this block we add the while statement along with
21
00:01:20.000 --> 00:01:24.000
our condition. That is i less than or equal to 5,
22
00:01:24.000 --> 00:01:28.000
followed by a semi colon. Now you might be wondering
23
00:01:28.000 --> 00:01:32.000
what is the difference between a while loop, and a do-while loop,
24
00:01:32.000 --> 00:01:36.000
do-while loops are always executed at least once.
25
00:01:36.000 --> 00:01:40.000
Even if this condition evaluates to false. Let me show you
26
00:01:40.000 --> 00:01:44.000
what I mean. So I'm going to temporarily comment out these few
27
00:01:44.000 --> 00:01:48.000
lines. And bring back our while loop.
28
00:01:48.000 --> 00:01:52.000
Also, we don't need this for block for now, so let's comment it out,
29
00:01:52.000 --> 00:01:56.000
now if you save the changes, we get 1, 3, 5 on
30
00:01:56.000 --> 00:02:00.000
the console. However, if I change i
31
00:02:00.000 --> 00:02:04.000
to 9, we are not going to see anything, because the first time
32
00:02:04.000 --> 00:02:08.000
we try to execute this while loop, this condition evaluates
33
00:02:08.000 --> 00:02:12.000
to false. So these statements are never executed.
34
00:02:12.000 --> 00:02:16.000
So save the changes, look, there's nothing in the console. So in
35
00:02:16.000 --> 00:02:20.000
while loops, this condition is evaluated ahead of time.
36
00:02:20.000 --> 00:02:24.000
At the beginning of every iteration. In contrast
37
00:02:24.000 --> 00:02:28.000
in do-while loops,
38
00:02:28.000 --> 00:02:32.000
this condition is evaluated at the end. And
39
00:02:32.000 --> 00:02:36.000
that means these statements are always executed
40
00:02:36.000 --> 00:02:40.000
at least once. Even if the condition is false.
41
00:02:40.000 --> 00:02:44.000
So let's try this, I'm going to comment out this while loop,
42
00:02:44.000 --> 00:02:48.000
and change i to 9, just like before.
43
00:02:48.000 --> 00:02:52.000
Save the changes, we get 9 on the console,
44
00:02:52.000 --> 00:02:56.000
why? Because in our do-while loop here,
45
00:02:56.000 --> 00:03:00.000
on line 15, we check to see if this is an odd-number it is,
46
00:03:00.000 --> 00:03:04.000
and display it on the console. Next, we increment i by 1, so i,
47
00:03:04.000 --> 00:03:08.000
is 10. Then, the condition is evaluated, of course it's
48
00:03:08.000 --> 00:03:12.000
false. So our loop will terminate. Now, realistically
49
00:03:12.000 --> 00:03:16.000
we're not going to use this do-while a lot in programming,
50
00:03:16.000 --> 00:03:20.000
there are situations you may want to use this, but in practical terms,
51
00:03:20.000 --> 00:03:24.000
most of the time you will be using a for or while loop. Just
52
00:03:24.000 --> 00:03:28.000
be aware of the difference between a while loop and a do while loop.
4557
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.