Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Use `do...while` to create a loop that executes code until the test condition evaluates to false.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
There's another type of loop in JavaScript
that's closely related to the while loop.
0:00
It's called the do-while loop.
0:04
Now open the file do-while.js and
copy the getRandomNumber
0:06
function from while.js and
paste it inside this file.
0:12
In index.html,
update the script tag source attribute
0:17
to js/do-while.js. The structure of
a do-while loop looks like this.
0:22
The do statement comes first and
is followed by a while condition.
0:29
Do-while repeats an action over and
over again, while a condition is true.
0:35
In other words,
the code you place inside the do code
0:41
block here will run as long as
this while condition is true.
0:45
That sounds pretty much like a while loop,
doesn't it?
0:49
It pretty much is.
0:51
But there's one important difference.
0:52
Do-while will always
execute the code block
0:55
once before the condition is checked.
0:58
That's because the condition isn't
tested until after the code block.
1:02
Let's compare the two.
1:05
Earlier, you used a while loop to log
10 random numbers to the console.
1:07
Here's the code you wrote.
1:12
Notice that with a while loop,
1:14
a condition gets checked at the very
beginning, before this code block runs.
1:16
If the condition is true,
then the loop runs.
1:21
If the condition is false,
the loop does not run.
1:25
The condition might be false right at the
beginning meaning the loop will never run.
1:27
Here's how you'd write this using
do-while I'll again initialize
1:32
a counter variable to 0, to keep track
of the number of times the loop runs.
1:38
Inside the do block,
1:44
I'll add the console.log statement
displaying the random number.
1:46
Then increment the counter variable by 1.
1:51
With counter += 1,
after each pass through the loop,
1:55
the while condition will be evaluated.
2:00
So between the parentheses I'll
add counter is less than 10.
2:03
Remember, do while does not
check the condition until
2:09
the code block has run once.
2:14
If this condition is true,
it runs again and
2:16
again until the condition
is no longer true.
2:18
In this case when counter is no longer
less than 10 I'll save my file.
2:21
And when I preview
index.html in the browser,
2:26
10 random numbers appear in the console.
2:29
Good.
2:31
So when might you use do-while over while?
2:34
Keep in mind that in many
cases you can use do-while for
2:38
the exact purposes as while.
2:42
Use do-while when you need your
loop to execute at least one time.
2:44
For examples, log at least one random
number to the console, then check if
2:49
counter is less than 10 before running
the loop again and logging more values.
2:54
Use while when you need to check
a condition before performing an action.
2:59
For example check if counter is less
than 10, before running the loop and
3:04
logging a value to the console.
3:09
Either way, you'll get the same output.
3:11
So the approach you use is
mostly a matter of preference.
3:12
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up