Welcome back.
In this video we're going to look at do while loops.
And they are very similar to while loops.
The only difference is that they start straight away.
So they don't need to first condition to be met.
They execute at least once and then they do the check whether the condition is met or not.
So let's have a look at what this looks like.
So there is this do keyword and it's a code snippet.
So press tab here and tab once again.
And then you will get this do while loop.
And that is how it's going to look like.
So you have this do keyword at the start.
Then you have the curly brackets.
And this is the code body that will be executed at least once.
That's the difference to the while loop.
And then you have this while keyword which checks whether the thing inside of the brackets is true or
not.
And after that you have the semicolon which finishes this do while statement.
So let's first create a counter which will be quite simple here.
So we have a counter variable which I'm going to set to zero.
And then I'm going to use that counter to display it onto the console.
So I'm just going to say counter here.
And after that I'm going to increment this counter variable by one.
Because otherwise there's no way for my counter variable to hit the not true value in here.
So while counter is less than ten for example.
So this counter will count from 0 to 9.
And now let's add a console read key here so that our application keeps running.
After it ran the loop you see zero, 123, four and so forth.
So it did the entire execution here.
And now.
The interesting thing is let's say counter starts with 15.
And we check our while statement here.
And we see 15 is not less than ten.
So this will not run or will it.
Well actually it will run once.
So you see it will give me the value of 15 once.
So this is a loop that will execute at least one time.
And we can see that here.
Even though this condition isn't met it will still run one time.
So the do while loop is a post test loop, while the while loop is a pretest loop.
So the do while loop runs before it does the test.
So the test happens.
Post execution.
There are a few situations where you would use a do while loop over a while loop, for example, and
that is when you need user input.
So if you want to do user input validation, in that case, what we can do is let's say we have our
number here.
Let's not call it counter, let's just call it number.
And we're not going to assign a value to it.
So by default it will be zero.
And what we want is we want the user to enter a whole number.
Enter a positive whole number which means one and up.
So not minus one not minus ten none of that.
And here we are going to override number with whatever the user has entered like so.
And then we can check his number less equal zero.
And as long as it is less than equal zero, we're going to run the loop until the user enters a whole
number it wants.
The user does enter a whole number we get out of the loop, because that condition here is not going
to be met anymore.
So we're basically waiting for the user to finally enter a whole number.
And once he does it, the loop is executing no more.
Okay, so let's run this.
Please enter a positive number.
I'm going to enter minus five.
I'm going to enter minus ten.
I'm going to enter zero.
And once I enter five you see the loop is over.
So you could add a line here saying finally okay.
So finally we entered a whole number that was positive.
So again minus five minus three for example.
And then five.
And then it says finally okay.
So this was just one example.
Obviously there are more situations where this makes sense.
But now you have a general idea as to how to do while loop works.
And we're going to see more examples over time.
So see you in the next video.