Welcome back.
In this video, we are going to look at another control, which is one type of a loop, and there are
multiple types of loops.
And we're going to start with a while loop.
So a while loop executes a block of code repeatedly as long as a given condition is true.
So we're looking at this wild, key word.
Then we have a condition in the brackets, in the square brackets and then in the curly brackets.
We have the code that should be executed as long as the condition is true.
So now if we look at the specific example, it could look like this.
So we have a variable called X and it should be one.
So it starts with the value of one.
And then we compare and say as long as X is less or equal to 10.
We want to execute the following code.
We want to say X, so we should just print the value of X, nothing more than that.
All right, so if we do it like this, then we get an infinite loop, because this wild statement will
always be true because one is always less than 10 or equal to 10.
So what we need to do is we need to at one point lead to an end for this condition.
So this condition has to end at one point.
It's really important.
It can either be done through whatever happens within the wire loop or it can be done from outside.
So let's say we execute something as long as the mouse is moving, as soon as the mouse is not moving
anymore, we don't execute it.
This is a very basic example and this is something that probably shouldn't happen.
But if you look at our mouse while we're moving it, it's also changing its position and our UI, even
though, of course, it will not be executed via a wire loop, how the mouse is displayed.
But it's going to be something that really is calculated all the time, meaning our screen so refreshing
the whole screen and displaying the position of the mouse at a specific point.
All right.
So this while Loop will be executed all the time until the point where the condition is not true anymore.
So we need to actively lead to an end of this condition, meaning that we can do something such as incrementing
X each time that this while loop is executed.
So what will happen now is our X is one in the first iteration, then this condition is checked.
So is one less or equal ten.
It's true.
So execute the code block.
And in the globe -- is just going to print X, whatever X is in our case one, and then it's
going to increase X by one, which means that in the next iteration is going to be two.
So we check is two less or equal ten.
It's true.
All right.
So please execute the code.
So it's going to print two and increment X by one.
So it's going to be three in the next iteration and that's how it's going to go until it's a ten.
So it's going to check ten less equals.
Ten is true.
All right.
So execute the block, print ten, increase X by one, which means X is going to be eleven now.
So we check is 11 less or equal ten.
No, it's not.
So don't execute the block any more, but go to the next line of code, whatever that will be.
So if we have something here while loop is done only then this while loop is done, code will be executed.
So let's execute this code and we see one, two, three, four and so forth, five, six, seven, eight,
nine, ten.
And while loop is done.
Now, if you want to print them next to each other and not line by line, you can, of course, instead
of writing print and just print it and it will print it just next to each other.
So the we are one, two, three, four and four, why is that?
Now, by the way, what if you want this while loop to be in the next line, so this texture while loop
is done?
Well, how you can do it is you can add an additional line break manually.
How would you do that?
Well, you do a backslash and here this backslash and is not going to be interpreted as an RN and as
a string, but it's just going to say or it's going to be interpreted as a code, so to speak, and
it's going to create a line break.
So as you can see, it's also orange.
It's not green as the string is, and that's generally what the backslash does.
It escapes the character.
So that's the key word here.
It says escapes or escapes the end, which means that it will be ignored as a string, but it will be
used as code, so to speak.
So if we run that again, then while Loop is done, will be on the next line of code or on the last
line of this case of our console, you can also see that this empty space here is also over here.
So you can even get rid of this empty space and it will still work.
So that we are now there is no empty space at the start of the line anymore.
So now a little challenge for you, right a while loop, which only prints every second value descending
from 100.
So it's going to print 98, 96, 94 and so forth.
All right.
So please go ahead and try that.
All right, I hope you tried it, so we started one hundred and.
We go down to zero.
And then instead of plus plus we say minus equal to.
All right, now it's going to print.
The value and it's going to go down by two each time, and maybe we even started well, this in this
case 100 will be included.
Otherwise, what we could do is go from ninety nine here.
But I said going from 100.
So let's just see what is going to print.
And we are one hundred ninety eight ninety six ninety four, 92, 90, 88 and so forth.
So it goes all the way down to.
Four, two and zero.
So and these examples, it's a very basic type of code that is executed.
It's really just printing something on the screen.
But this can be super useful and many different applications when writing code.
So while loops are not only useful for what you can see here, what we just print a value onto the screen,
but they're really useful to just execute a code multiple times.
And yeah, that's something that we're going to do quite often in the future and we're going to use
different loop types.
So not just a while loop, but also the for loop or even the do while loop and of course also the for
each loop.
So I'd say let's just go to the next video where we're going to look at the next loop type and we're
going to look through all of them step by step in this course.