(upbeat music)
- In JavaScript,
we can create a while loop using the while statement.
In a while loop,
we need to create a condition that needs to remain true
for the loop to execute.
First, let's create a value to test within the loop.
Our loop will go through each integer from 0 to 99
and find all numbers that are evenly divisible by nine.
Let's create our new variable.
var myValue = 0.
We first start with the while statement.
Then we create our condition test
that will need to be true for the loop to continue.
Since we're testing for all values below 100,
we can enter that in as myValue is less than 100.
Then we create our code block for the loop.
Inside of this,
we're going to use a new mathematical operator
called the modulo operator.
Modulo takes a value and divides it by another number.
If there is a remainder, it returns that number.
So for instance, if we tried to divide nine by nine,
it divides evenly.
So the result is zero.
If we try to divide 10 by 9, the result is 1,
since after you attempt to divide 9 into 10 evenly,
there is a remainder of one at the end.
In this test, if a number is evenly divisible by nine,
then the answer will be zero.
So we'll use an equality evaluation to test
if the mathematical expression of myValue,
when divided by nine, has a remainder of zero.
If it does, we'll print that value in the console log.
At the end of the loop we'll then increase the value
of myValue by one and test it again.
But here is another shortcut that I want to share with you.
myValue = myValue + 1
is the same as myValue += 1.
But if you're only increasing the value by one,
there is an even shorter shortcut, myValue++.
This is called the increment operator,
and it is directly added to the end of the variable name.
And all it does is increase the value by one.
There is a matching (indistinct) operator,
that has a double negative sign,
that decreases the value by one.
With that, we close the loop.
Let's save and load in the browser and see what we get.
When you run, you'll see that all the values
that are evenly divisible by nine
are displayed in the console.
As soon as the value of my value becomes 100,
that renders the conditional tests at the top false,
so the loop ends.
There is a variation on the while loop
called the do while loop.
This has helpful if you need to make sure
that the loop runs at least one time.
Let's create a new variable at the top
that we know will evaluate as false.
var myBadValue = 0.
Then we create the loop using the do statement.
But we don't put a conditional test here,
we put that at the end of the loop.
We can create our code block,
and then send a message to the console:
"This will run at least once."
After the code block, we'll add the while statement.
And here is where we'll put the conditional statement,
myBadValue != 0.
We'll create additional tests that will evaluate as false.
But since this is a do while loop,
even though the test is false,
the loop will run at least one time.
Save and reload the browser.
You'll see that the message in the loop displays only once,
because the evaluation at the end is false.
Using the while and do while loops
you can run sections of code over and over again.
The while loop will only run
if the conditional test is true;
the do while loop will run at least once
and then repeat for as long as the conditional test is true.
(upbeat music)