In this video, we'll look at the last looping construct in c++.
It's hard to do while loop.
The do while loop begins with the do keyword followed by block
statement than the wild keyword followed by the loop control
expression in parentheses.
Notice that you need a semicolon at the end of the do while statement.
In a do while loop, you execute the body of the loop while
the conditional expression is true, pretty straightforward.
Note that the condition is checked at the end of each iteration that makes
the do while loop a post-test loop.
It also means that you're guaranteed that the loop body
will execute at least once.
This comes in very handy in many cases.
We don't have to force our way into the loop like we had
to do with the while loop.
Let's see an example of using the do while loop for input validation.
In this example, we execute the body of the loop then test a condition.
So we asked to use it for a number between 1 and 5, and
assign it to the variable number.
Then we test to see if number is out of range.
If it is, then we loop again.
If it is in range, then the loop terminates.
You can see how much cleaner the logic is in this case.
Also, let me talk for a minute about the variable number.
Notice how we declared it outside the loop body.
Since number is used in the while loops condition expression at
the bottom, it cannot be declared within the body of the loop.
If you do so, you'll get a compiler error.
In this example, we're declaring selection as a character type.
It's outside the loop because we're using it in the loop
condition at the bottom.
So let's look at the loop.
I always start reading loops at their conditional expression.
In this case, we know We're looping while the selection is
a lowercase and uppercase y.
If the selection is any other character, the loop terminates.
Now let's see what's going on in the loop body.
We see we have 2 doubles with an height declared and initialized to 0.
And we asked to use her for these values and read
them in from the keyboard.
Then were calculating the product of the width and the height
and initializing another double named area with that result.
We display the area to the user, and then we asked if
they want to calculate another.
Note that we have executed that first generation of
the loop unconditionally.
That means we always execute this loop at least once.
This makes sense in this case since I want to be able to calculate the area.
Now we ask the user if they want to calculate another, and we read the
response into the selection variable.
Then we test this variable in the loop condition.
If they entered a lowercase or an uppercase y, we perform
another iteration of a loop and calculate another area.
If they answer anything else, the loop terminates and we display thanks.
The choice between a while loop in a do while loop is
it always crystal clear.
However, if you know that you must perform at least one iteration of the
loop, then you should consider the do while loop as your first alternative.
Let's head over to the IDE, and we'll see how we can use a do loop
to do a real simple menu system.
Okay, so I'm in the IDE, and let me share an example of the
application we're going to build.
I'll run it now, and you can see it's a simple menu-driven application.
There's the menu, press 1, 2, 3 or Q.
If you press 1., you do this.
If you press 2, you do that.
If you press 3, you do something else.
Obviously these selections could do any kind of behavior
depending on your application.
So in this case, I enter selection, I press number 1.
And it says you chose 1, doing this.
If I press 2.
It says you chose 2, doing that.
I'll press 3.
You chose 3, doing something else.
Obviously, if I press a lowercase q or an uppercase q to quit, but anything
else should give me an illegal entry.
So let's say I pressed 4, it says your unknown option, try again.
Let's say I I pressed Pete.
Again, unknown option, try again.
The only valid choices will be 1, 2, 3 in lowercase or uppercase Q.
So now if I type of Q, the program terminates and says goodbye.
Okay, so that's what we're going to build.
And in the section challenge at the end of the section, you're going
to be using this kind of logic to build a more complex application.
All right, so let's start.
Where do we start?
Well, we know we need to display a menu at least one time,
right because the user has just like Q or x or whatever
we choose is the exit criteria.
So that's a good choice for a do while loop.
So basically, we're doing the do part which is display the menu while
they haven't quit, makes sense.
So that's what we're going to build.
Obviously, we need a character variable, and
I'll just call it selection.
And whatever the user types in will be stored into that character variable.
Now let's do the do logic here.
We've got a do loop.
And we want to do the body of the loop while a condition is true.
Now what is that condition?
Well, we want to loop as long as they haven't typed
a lower or an uppercase q.
So we can do something like while selection is not equal
to a lowercase q and selection is not equal to an uppercase q.
Okay, if you were thinking or here, that would not be right
and you would test your code and see that that doesn't work right.
So let's just write a real simple test here before we
even bother with anything else.
Let's just say cin selection.
No, man, you know promptly no nut.
We're just going to grab a bunch of characters here and see what happens.
So let's run that.
Okay, let's type in an a.
We loop up q, we stopped.
Cool.
That's what we want.
Let's try that again.
A 1, we loop.
A 2, we loop.
A 3, we loop.
A 4, we loop, again.
The only time we should stop is when we get an lowercase
or an uppercase q, we stop.
So our logic is correct here.
Now what we can do as we can actually prompt the user.
And with the menu, we can say cout, and let's just use put a
new line at the beginning, so it gives us a new line every time.
We do a dash of characters here.
And will do that.
And then we'll give them the menu option.
So you can press 1.
And if you press 1, I'll go through this.
Obviously, this would be add a record or delete a record or do
something in the real application.
So that would be that.
And I'm just going to copy this and paste it to save a
little time here, so number 1.
Number 2 would be do that.
Number 3, eruptions will be do something else.
And then finally, we can say Q or or x or whatever letter you like to quit.
Perfect.
And we'll give them one more little prompt here that says.
and we'll put a new line in front just a separate,
we'll say enter your selection.
A colon, no endline here, so that looks nicer for them.
And then we can put the selection, right at the selection, right.
So let's run this and that we should see a menu show up.
And there you go.
Do this, do that.
Do something else or quit.
So if I press 1, we stay in the loop, and we display the menu again.
Same thing with 2, same thing with 3.
4 gives us the same issue cuz we haven't checked for
anything, this illegal, right.
5P whatever.
Now whenever I temper Q.
I stopped.
So what I need to do now is decide what do user has
typed in and what to do?
Okay, how do I do that?
Well, it's a lot of choices here.
We've got literals here.
We got 1 the 2 the 3.
These are character literal
So we could certainly use a switch statement really, really easily.
What I want to do is I'm going to use an if else if ladder here.
Again, I encourage you to do this with a switch statement with all kinds of
logic that we can play around with it.
So let's do this.
Let's say if the selection that the user entered is the character 1.
Remember, this is a character, so we need the single quotes.
So if the user entered the 1, we simply want to
say something like cout.
Let's see "You choose 1 which was do this or let's just say doing this.
Put endline at the end.
Else and we don't need block statements here.
We could certainly use them and makes it easier to modify our code.
But I wanted to try to keep as much on the screen as I can.
So else if the selection is a 2.,
then what we want to do here.
And again, I'll just copy and paste this to save a
little bit of typing time.
Then you spell that correctly.
That's better you chose 2, which was doing that.
Else and remember your indentation they could.
Make sure the indentations correct as it adds to the readability big time.
So if the selection is equal to 3, then in this case, you chose
3, which was doing something else.
So now what where we at.
Let's worry about the unknown option as the bottom else of this condition.
So if nothing else matches, then we're going to say, sorry try again.
But in this case, if you really think about this 1, 2,
3 and Q are all valid, right.
So let's deal with Q.
I'll say else if selection is equal to a q or another sit here.
We need an or where I lis10 to while we need to demand or selection
is equal to or lowercase q.
Then 'in this case really only want to say is goodbye because
the loops is going to fail.
I'm going to be out of here in a minute.
Okay, so that takes care of that case.
So what's left.
This else to here.
So what just happened?
Well, it's not a 1.
It's not a 2.
It's not a 3.
It's not a q.
It's got to be something that I don't know and I really don't care.
It's something that's illegal.
So this case we'll just say cout.
Let's say something like unknown option.
You spell that right and maybe try again.
We could do something like that.
Let me clear the space up and let's walk through this and make
sure we're not missing anything.
We've got our selection variable declared that's important.
We're in the do loop.
We're going to execute this code immediately, right.
This is unconditional the first time in we're in.
So we're displaying the menu, and we're reading the
selection from the user.
If the user selects 1, you chose 1 doing this.
And let's say we're out of this if else if ladder.
We test down here.
And we loop while the selection is not lowercase or uppercase q.
We come back up.
We display the menu again if they typed a 3.
We execute this code right here, and we keep looping.
If they type Q, what do we do?
We simply say goodbye.
And then this condition Fires at the end, it'll
stay longer out of the loop.
If none of these are true, we just say unknown option.
I don't know you've entered.
Okay, so let's try this.
And again, I'm in the section 9 workspace in the do while project.
So let's build and run.
And we got to clean compile and run.
So let's try this, press 1, do this.
You chose 1 doing this.
Exactly what we want 2.
You chose 2 doing that.
3, you chose 3 doing something else.
Let's say we pressed 4, unknown option, try again.
But notice how we don't get out of the loop.
We don't want to get out of the loop, right?
We're going to ask them to enter Q if they want to get out.
So at this point, we can press an uppercase q.
And there it is.
Goodbye and the program's finished.
Okay.
Really, really common use case is this idea of menu-driven programs.
You want to display that menu the first time, ask
the user for this election.
And basically, using if else if ladder here or switch
statement would work here.
I'll try re-reading this program using a switch statement.
You'll see it's pretty easy to do as well as.
Same logic really.
Okay, so that's pretty much it.
In the challenge exercise at the end of the section, we're actually going
to do stuff when the user presses 1.
We're going to use a vector.
You'll be adding things to the vector and displaying the
vector elements and so forth, depending on what they choose.
Okay, so that's the end of this video.
I encourage you, like I said, walk through this and we getting change it
and change this and see why it doesn't work, understand that that's really,
really important. That's a very common mistake.
All right, I'll see you in the next video.