In this video, we're going to start to talk about loops,
specifically while loops.
And now, loops are just incredibly important to all computer programming.
It doesn't matter what programming language you're using.
You're always gonna use loops.
They are probably just as important as if else statements, and
that you're gonna use them all the time.
There's a couple of different loops we're gonna look at.
We're gonna look at the while loop and for loop.
In this video, we're gonna focus on the while loop.
The difference between the two is sort of trivial, and
in many instances, you can use any kind of loop you want but
I find that programmers tend to gravitate towards a certain loop.
Me personally, I like to use while loop so I tend to use them all the time.
I could easily use a for loop, instead of a while loop, it's just
a personal preference, so you'll find the one that you like to use the most and
that's the one you'll use for most circumstances.
Now there are some times when you just have to use a while loop or a for loop or
whatever, but it's not all that rare.
So anyway, a while loop looks like, while condition do something.
Now, this looks an awful lot like an if statement.
And in fact, if we change the word while to if, we would have an if statement.
So if you know how to use an if statement, you're already sort of familiar with this.
That's probably why I like to use the while loop, it's just familiar.
So here, we can use our comparison operators.
We can do all kinds of stuff.
And in fact, let's just create one.
So to use a while loop, first we have to set a counter.
And I'm just gonna call it x.
I'm gonna set it equal to zero.
And I'm gonna say while x is less than ten, while this is true, do something.
And let's echo out, your number is x.
And I'm gonna put a line break here.
So it put this on, it's online, okay.
We also need to increment our variable, and to do that,
we use our increment operator that we already learned, and that's just x++.
Now, why do we need to do that?
Well, what happens is, this executes and it checks this condition.
As long as this is true, it'll do this next line.
If we didn't have this here, x would stay zero forever.
And it would loop back round, and it would say zero is less than ten.
It would echo this out.
It would loop back around, zero is less than ten.
Echo loop, zero less than ten, echo loop.
And you get what's called an infinite loop.
It would just keep going forever and ever and ever.
And when we print out, your number is zero a zillion times on the screen, and
your program would crash.
To this day, I forget to do my increment operator on here every now and then, and
my programs crash.
It's just one of the pitfalls of using the while loop.
But it's gonna happen to you, don't worry about it.
It's not a big deal.
You just restart your program and you go I forgot my increment operator.
So let's save this, come up here, and hit reload and
we can just see your number is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
And then it stops.
Why does it do that?
Once it hits 9, 9 is less than 10, so it prints out your number is 9.
Then it increments, and the 9 becomes 10 and then we loop again and
10 is not less than 10, so the whole thing stops.
This thing continues to loop while this is true, but
10 is less than 10 that's false so the whole things stops.
So that's pretty much it, that's a while loop.
There are some variations on the while loop specifically the do while loop,
and you can look up how to do a do while loop.
I've never actually used one.
This while loop will get you through most of a looping that you'll ever need to do.
In the next video, we're gonna talk about for loops.
It's very similar to a while loop, just slightly different and
you'll see all about that in the next video.