Now that you have seen a while loop as a counter, let's use this while loop for where it really shines
and that is that you either have a boolean statement here, so either you have a bool is good.
For example, let's set that to false and then you use is good in here.
So either you use a boolean directly in there or you can use a statement that is true inside of this
condition once, but then becomes untrue inside of the while loop.
So let's have an example here.
So let's say I am setting is good here to false at this point.
And by default it is set to true.
So what this code will do is it will check is good true.
And if is good is true then it will run.
This code then is good is going to be set to false.
And then it stops running the code okay.
So it stops running this statement and there's nothing to display.
So life is good and suddenly it becomes bad at line eight for some reason.
Okay.
So our code is going to be saying life is good and then it gets out of the loop.
So how can we use this more sensibly.
Because this is a really trashy example right.
But I just wanted to show you a very, very simplistic example.
So alternatively what we can do is we can have a string here.
Let's call this one user choice.
And I'll have a really cool little example in the next video.
But here let's say user choice is console dot read line.
And now we're using user choice.
And we're saying as long as user choice is go, then we are going to execute whatever is inside of those
curly brackets.
But we need to make sure that we change the user choice.
So therefore we can use the console read line statement again to change what the user has entered.
So what I'm going to say is I'm going to start with the console right line statement and give the user
the choice to go or to stay.
So go or enter go or stay okay.
So if the user says go then this while loop will run.
And we're going to say go for a mile okay.
And then we say we could have obviously also set something like walk.
Maybe that would have been better.
But here I'm going to say Console.WriteLine.
Wanna keep going.
Enter go okay.
So now if we run this again we will see it says enter go or stay.
So if I enter stay then the software basically does nothing.
It closes.
So we could say something like finally you are staying.
Let's run this again.
Enter, go or stay.
I'm going to say go.
Go for a mile.
Want to keep going?
Yes, go.
I want to keep going.
Yes.
Want to keep going?
No.
Finally you're staying.
So we could have entered.
Stay.
We could have entered whatever we wanted.
I didn't have to enter, stay or whatever at this point, because that's not what we're checking.
We're not checking for stay.
We're just checking for go.
And as long as go is true, this while loop will run as soon as go is not true anymore.
This statement is not going to be true.
So it gets out of the loop because this condition isn't met.
So when the condition is met, it does not execute this part, but it jumps directly to line 13.
And we can test this real quick with our beautiful little breakpoint.
Just so you see how the code is executed here.
So we run our code.
We say enter, go or stay.
I'm going to say go.
And then we get to this point where our program is paused.
So we go into the next line.
You see user choice is go which is the same as this statement.
So it runs the code here.
So then it runs each of those lines.
Now it asks again the user for an entry.
So want to keep going.
Enter go.
So I'm going to say stay.
But therefore I need to step over basically running this line.
And they see stay stay.
So I'm going to say stay.
And now it checks again is user choice stay which is the value of user choice.
Is it the same as go.
So the next line that will be executed here after this choice will be the line 13.
Because this statement isn't true anymore, the code in the brackets is not executed, and that's pretty
much what the while loop can be used for.
Now, you can also obviously set a bool and check if user choice is so and so.
Set the bull to true and then you can check for the bull here.
But we can also just have a boolean expression in here.
Or basically a condition check in.
Here's where we compare strings against each other, or where we compare numbers against each other
or whatever.
So the logic is the same.
But basically that is the structure that you need to use for a while loop.
You need to have something that can be checked.
So a condition you need to execute whatever you want to execute inside of the loop.
And then you need to make sure that there is a way out of the loop, which means that there is a way
to change the condition that is requested here to be false at one point, because otherwise you will
have an infinite loop.
All right, so that's it for this video.
In the next one I have a really cool little project for you.
So see you there.