Welcome back in this video, we are going to look at another control flow, 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 while keyword.
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 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 whale 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 while 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 wire 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 block it's 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 to a less or equal ten.
It's true.
All right.
So please execute the code block.
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 at 10.
So it's going to check 10 less equal.
Ten is true.
All right.
So execute the block print 10 increase X by one, which means X is going to be 11 now.
So we check is 11 less or equal.
10.
No, it's not.
So don't execute the block anymore, 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 10.
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 though we are one, two, three, four and four while Luke is done.
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 end 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 just keeps the character.
So that's the key word here.
It says escape, so as keeps 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 next
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 start at one hundred and.
We go down to zero.
And then instead of + +, 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 start at.
Well, this case 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 there we are 198, 96, 94, 92 in 1988 and so forth.
So it goes all the way down to.
For two and zero.
So and these examples, it's a very basic type of code that is executed, this is 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, where we just print a value onto the
screen, but they are 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 to do while loop.
And of course, also the for each loop.
So I 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.