Welcome back to Learn to Code and JavaScript in this lecture.
We're going to talk about looping.
So with conditionals, we made logical decisions about which path we're going to take with looping.
We make decisions about repeating code deaths.
So essentially, we're going to do something repeatedly while a condition is true.
So some examples of how we do looping, there's a for loop, there's a while loop in a do while loop.
So here's some examples for Loop is more complicated than the ones with the word wild in them because
a for loop, the first of all, there's a couple of different variations of for loops, but the most
traditional basic for loop is a four letter.
A variable started a value continue until it's reached a value and then add something to it each time.
So in this case we're saying let ay equals zero.
So we're going to set a variable ay to be the value zero and then we're going to start the loop and
we're going to start the loop of is less than ten.
And then at the end of the iteration we're going to do something and then we're going to increase the
value of it by one with the plus plus and then we'll go back to the beginning and try again as I still
less than ten.
Yes.
OK, so we'll do something that will increase the value of EI.
So that's the most basic traditional for loop you can just for a loop so you can have a for loop inside
of a for loop and think of this as two dimensions.
So if you're going through every square instead of a larger square, you might be going across the column
squares and you might be going down the road squares.
So think of a bingo card, if you're remember with that, where you have a board that's broken into
five by five squares and so you go across the the five in the columns and then down each one clear across
all five as an example.
And one of the key words you can use here is to continue.
So what the continue does is say at this condition is true.
Just continue with the loop, go back to the beginning and start again, essentially.
So if the condition is true, we're not going to do the second something.
And in this example, with the if there's a break statement that says, hey, if the condition is true,
break out of the loop.
And incidentally, you don't have to use this in conjunction with the condition, you could just say
break and it wouldn't do the rest of the world or continue and would always continue back to the beginning
at that point, which is most commonly done with enough.
Those are the type of variation on the for loop, which is a form of with which lets you iterate something
such as an array.
So if we have an array with two, four, six, eight, 10, and I said let a equals that array, I could
say let the of a.
And then this is going to take on all the values in the array.
All right.
Let's take a look at the while loop examples.
So the basic while loop just checks the condition at the beginning like an F check, the condition in
it is true.
It's going to keep executing.
So you'd better do something to make it false eventually.
So in this case, I'm saying if execrated zero, continue the loop and then we'll subtract one from
X.
So that started out as ten.
It's going to be ten, nine than eight.
And then once it gets down to zero, it's going to stop executing.
The doodler is like the wild loop, it checks a wild condition at the bottom instead of the top.
So you always enter the loop on the while loop, if the if X is starting value was zero, you would
never enter the loop.
But on the do you always enter the loop for at least one path?
So let's take a look in visual studio code as some examples of looping.