Welcome back.
In this video we're going to look at the while loop.
And the while loop is just a different type of loop.
And it is different in the sense of how it can be used as well as where it is most powerful.
So let's start with a very simple example.
Let's rebuild a for loop that we have built in the last video or in the last few videos.
So we have this for loop that just counts to ten okay.
So I'm going to display I here okay.
I'm just going to say right I on the screen.
So we've seen how that works right.
So let's use console dot read key to keep it awake.
So to keep our software running and we see just counts from 0 to 10.
Now we can do the same thing with a while loop even though it's not what the while loop is perfect for.
But let's still just quickly look at that because you might see that as well.
So we have an int counter that I'm going to set to zero.
And then I'm going to use this while keyword.
And you can see it already says while counter less than ten I'm going to say counter plus plus.
And inside of here I can write out the counter.
So this piece of code here will do the same thing as this piece of code.
So the for loop you see is simpler.
And for this particular example the for loop is the better choice.
But you see we are using the while loop to do the same thing where first we set a counter variable like
we do here.
Then we check a condition like we do here, and then we increment by one what we do here.
And before we increment we run this piece of code.
It's the same thing that we do here.
So now we're using this while loop in a way how we could use a for loop.
And it's going to work the same way.
So let me get rid of the for loop and let me run the software.
And you see it counts from 0 to 9.
Now what happens here internally.
Well it creates a variable.
Then it checks for the counter.
Is it less than ten.
As long as that's true this will be executed.
So this code block.
And then we run whatever is inside of it.
So we display the counter variable.
And then what's really important is we increment.
So what you generally just need in a while loop is here.
You need a condition.
And here inside of the curly brackets you need a way for the condition to not be true at one point.
So that means that if you don't have this line here, then you would run into an infinite loop because
zero is always going to be less than ten.
So if you run this let's see, you see we have this infinite loop which just keeps going and going and
going until the memory is like, uh, well, basically overloaded or something like that.
So this is an absolute no go.
You cannot do that.
You will crash your software if you do that.
So infinite loops are a real problem.
And that's why you need to make sure that inside of the curly brackets you have a way to make this condition
untrue.
So how do we make this condition untrue.
Well, we could increment the counter.
We could also just say counter equals 15 for example.
And then this would be run exactly one time you see zero.
Or we could say counter plus equal five.
So then we wouldn't go with single steps, but we would go with bigger steps with five point steps so
to speak.
So from zero to 5 to 10 and ten is not less than ten.
So this execution stops at that point.
So let's actually run this with a debug here.
So we get into our program we see counter is zero.
Let's jump into it okay.
This condition is true because counter zero is less than ten.
So we can use shift F9 here again.
And it says this statement here is true which is why it gets into this point.
So then at this point counter is still zero.
We display counter onto the console.
We increment counter by five and at the next point you see counter now is five.
And if we check this statement here again shift F9, we can see that five less than ten is still true.
Then we run it again.
And now we check once again.
So is this statement.
And let me actually mark this shift F9.
And you see counter less than ten is set to false.
That's because counter at this point is 1010 is not less than ten.
So it stops running at this point.
Or let's say our code directly jumps to this console read key down here as you see.
Because this.
Condition isn't met.
So this code will not be executed and it just goes to the next executable line.
Okay, so that's how we can use a while loop as a counter.
But in the next video we're going to look at where the while loop really shines.
Okay so see you in the next video.