In this video, we'll see how we can nest if statements
within other if statements.
Many times the logic we need to use in order to solve problems is more
complex than just an if else decision.
Many times we need several layers of logic.
Since an if or if else statement is a statement, then we can use it in
c++ anywhere a statement is legal.
So in this case, we can replace the statement part of the if
statement with an entire if else statement if we want.
That's exactly what this example shows.
Notice the indentation.
It becomes even more important now.
In this case, we can test multiple complex conditions easily.
So we evaluate expression1.
And if it's false, we skip everything.
But if it's true, then we test expression2.
And if it's true, we execute statement1.
If it's false, we execute statement2.
We can continue by including if or if else statements as we need.
Notice that we have two ifs and only one else.
In computer science, this is called the dangling else problem.
And every programming language has to decide how to handle it.
The problem is which if does that else belong to.
In c++, an else belongs to the closest if.
Of course, we can use block statements to achieve any logic we want.
Let's see another example.
In this example, we check if score is greater than 90.
If it is not, then we skip down to the bottom output statement,
and we display sorry no a.
If the score is greater than 90, then we check the nested if
statement condition and check if score is greater than 95.
If it is then we display an A+.
If it's not, we display an A.
In this slide, we can see a flowchart that visually shows the flow of
control for the logic in this example.
Notice the first condition is checked, score greater than 90.
That's the expression in the first diamond on the left.
If true, then we test the condition in the nested if statement,
which is on the true branch.
Of course, we can also use block statements as required
to execute multiple statements.
In this example, we're checking if Frank's score is
not equal to bill's score.
If this fails that means that their scores are equal.
So we go to the else statement and display looks like a tie.
If their scores are not equal, then we check the condition
the nested if statement.
And if Frank's score is greater than bill score, we display Frank wins.
Otherwise, we display bill wins.
In these examples, the nested if has been in the true section
of the if else statement.
We can have nested if statements in the else section as well.
Let's go to the IDE and see some examples of nested if statements.
Okay, what I want to do next is I want to open up the grades project
within the section 9 workspace again.
In this case, the main is empty.
And I'm just going to get rid of this piece right here just to
print out a new line at the end.
Okay, so what we're going to do here is this program is going
to ask the user for the score on an exam between 0 and 100.
And we're going to tell them the letter grade, similar to what we
did in the slides but we want to validate to make sure that what they
entered is actually between 0 and 100.
And if it isn't, we're going to tell them sorry, you need -- the
number you entered is out of range.
Also once we determine the student's grade, we'll tell them your grade
is your letter grade A B C D F.
And if they get an F we'll, tell them sorry you have to repeat the class.
Okay, so let's start here.
First off, we need a couple of variables.
Let's just use one called score that's what we're going to ask the user for.
And we'll cout.
Let's do the input, output part really quickly.
We'll just say enter your score on the exam.
And we'll actually put 0 to 100 here, so they know what the range is.
And we'll grab the input into score.
Okay, so now we've got what we want.
We also would like to have a variable called letter grade.
And what we're going to do is we're going to assign A B
C D or F into that variable.
We can simply use a character variable that'll be fine.
So I'm just going to say char letter grade,
and we'll just initialize it to 0.
Okay, so now first thing we need to do is we need to validate that score.
We need to make sure that the score is between 0 and a 100.
So we'll use a simple if statement here.
So we'll say if the score is greater than or equal to 0, and the score
is less than or equal to 100.
I'll use a block here, we need to do the true part, right.
This is where we need to calculate their grade.
But if it's not, then right here we want to say else, and
I'll do another block statement.
And in this case, what we want to say is sorry, we'll say sorry,
and we'll tell them that the number they entered
is not in range, something real simple.
When we learn about looping in a couple of videos from now, we'll be
able to ask them to enter it again.
But in this case, this will be fine.
And let's test this.
A lot of times when we test our code you see there's
nothing in here right now.
So we just do something like this, and we expect to see
that output in the positive case, right, in the true case.
We're going to write code in there to actually calculate the grade,
but it's good to test as you go.
Don't write all your code, all at once and then try
to test it all at the end.
Just test as you go, and you'll be much better off so.
Let's try this. Let's run this.
We got an error here, sorry.
Num it's score.
There we go.
So there we go.
Enter your score on the exam.
Let's enter something valid, like 70.
We should get the x's displayed.
There they are.
So we're good here.
And let's run it one more time, and let's type in 120.
Sorry, 120 is not in range.
And let's try something like negative 1.
Sorry, negative 1 is not in range.
Cool. So our logic seems to be right.
Now what we need to do is write the logic in this piece right here.
That's actually calculating their grade.
All right, so what we can do here is we can write the if
else if else if type code.
When I learned this stuff a long time ago that was called an if else ladder.
I haven't heard that term in a long time but it makes kind of sense
because it does look like a ladder.
So what we'll do here is we'll say we'll use that same
grading scale 90, 80, 70, 60.
We'll say if the score is greater than 90 and I'm not
going to use blocks here just to save some space on the screen.
We'll save the letter grade, and we'll assign an a to it.
Now notice that's a character so those are single quotes not double quotes.
So we've got that then we'll say else, if score is greater than 80.
We'll say that our grade is a B again single quotes,
don't forget the semicolon.
Else if you get the idea we're just walking right down that ladder.
If it's greater than 70, then the letter grade in this case is a C.
A couple more.
Else if the score is greater than 60, then the letter grade is a D.
And finally, there's our catch-all else in which case
their letter grade is an F.
Okay, let's try that out.
Let's run this, and we'll test it with a few numbers.
And I said greater than 90.
I probably should have said greater than or equal to 90 but it's all good.
So we'll say 91.
That's definitely an A.
We never printed it out, so let's do that.
This is why we test, right.
Cout your grade is and we'll just display letter grade here,
put an endline at the end.
Okay, now remember, I'm going to put this here and fix that B to a C.
This will execute no matter what.
So this if statement will execute.
One of those letters will be assigned, and then we display it here.
All right, let's try that again.
So in this case, we got a 91, that should be an A.
Your grade is an A.
The 81 should be a B.
It is.
Let's do about a 50, which is definitely an F, and here we go.
Okay, obviously, you would test this a little better, but for the sake
of time, I'm not going to do that.
The last thing we want to do is right here, we want to tell them
did they pass or did they fail.
Well, the only way you can fail in this example is if
your letter grade is an F.
So there's no sense in checking the score again.
We've already got the letter grade.
So I'm simply going to say if the letter grade is equal to an F.
Remember, double equal sign not a single equal sign.
If that's the case, cout sorry, you must repeat this class.
Okay, otherwise, you passed.
Congrats.
That's a passing grade or whatever you want to enter in here.
All right, so that should do it let's go through this
real quick one more time.
We've got their score here.
We're prompting for the score, we're reading it in.
If the score is within range, we do the calculation.
If it's not within range, we're down here, and we're out right away.
Sorry, the score's out of range.
Let me clean that up a little bit.
If the score is within range, then we're going to
use this if else ladder.
And if it's greater than 90, it's an A.
Greater than 8 is a B we're.
Going to walk all the way down.
You know what let me put equal signs here because it's bugging me.
That way it matches the slides that I gave you.
Okay, so that's a little better.
So now if it's greater than equal to 90, you get the A
and all the way through the F.
Then when we're here your grade is your letter grade.
And then we check to see if an F is just an F, you must repeat the class.
Okay, let's run this now with a couple of uh test cases.
Suppose you got your 90 now.
It should be an A because we have the greater than or equal to.
Your grade is an A, congrats.
If your grade is a 59, your grade is an f, sorry you
must repeat this class.
Okay, so there you go.
That's a simple example of using the if else if and the
if else statements together.
On the next example, we're going to open up the shipping project
in the section 9 workspace.
So we're in the shipping project of the section 9 workspace.
And I've gone ahead and started this little example, just
so we'll save a little time.
And what we're doing here is, we're doing.
We're using a shipping cost calculator.
We're going to ask the user for the package dimension in inches
and the package dimensions would be length, width and height.
And they're all integers.
The dimension must be 10 inches or less for each dimension.
Otherwise, we can't ship it.
The base cost for shipping is two dollars and 50 cents.
And if the package volume is greater than 100 cubic inches,
there's a 10% surcharge.
If the package volume is greater than 500 cubic inches,
there's a 25% surcharge.
Okay, so I started this already, and I've got some variables set up.
And let me talk about them really quickly.
We've got obviously, we need the length the width and the height.
That's what the user is going to enter.
And we've got the base cost which is a double at $2.50.
So I've initialized that.
Then we've got a couple of other constants here.
One, the tier one threshold.
That's the volume.
So again, if you exceed 100 cubic inches, that kicks in.
And we've got the tier 2 threshold to be 500 cubic inches.
So I've defined those as constants.
The maximum dimension for any length is 10 inches.
And I've got the tier 1 surcharge at 10%.
And the tier 2 surcharge at 25%.
So that's pretty much it.
I've got all these constants and variables declared
so that I can use them.
All right, I also have another integer here called the package volume,
which obviously we need to calculate.
So this is where we're at.
Hello, welcome to the package cost calculator.
Enter the length, width and height of the package separated by spaces, and
we've just read the length, the width and the height into these variables.
Now we need to do our logic for the calculation of the package.
First thing we should do is, let's make sure that
the package is valid, right.
Otherwise, there's nothing to do.
So what we can do here is we can say if the length of the package is
greater than the maximum dimensions, right, the max dimension length,
which is 10 for everything or the width is greater than the
max dimension or the height is greater than max dimension, right.
If any one of those is greater than the maximum allowed
dimension for any one side, we really have nothing to do.
We're done.
So in this case, what we want to do here is we want to say cout
sorry package rejected or something like that,
dimensions exceeded.
Okay? Or some message that tells them what's going on.
Okay, so that takes care of that.
Now here's the else part, right.
And we may as well put this in a block because we never know when
you're going to add code to this.
So I'll put a curly here and a curly here.
Now I can add code in there easily.
So that's my if part.
This is when the package has been rejected.
The else part is now where we're going to do all our calculations.
So again, I'm going to do cout and do a quick test to make sure that we're
compiling and everything looks good.
So I'm going to run this.
And I'm running the old project, let me double click on
shipping, and let's try that.
Enter the length, width.
Okay, so let's say I'm exceeding, right.
So I'm going to say 12, 8 and 12, 5 and 3.
So the 12 should be over.
Sorry package rejected, dimension exceeded.
That's good.
We could do test cases for all three of them, right.
I'm not going to just to save a little time.
And in this case, 6, 7 and 8, that's valid.
And we print out the three x's, and we're ready to do our calculation.
Okay, so let me get rid of that statement that I just did and that
hello world down here at the bottom.
All right, so now in here, what we need to do is our calculations.
So we need to know what the package cost is, right.
So we need to have a double in here.
We can do that right inside the block because the only time
we're going to use it is if we're going to do some calculation.
So in this case, I could just say package cost and I can
just initialize that to 0.
And how about the package volume.
Again, I could have defined that in here as well but
it's already defined outside.
The package volume is the length * the width * the height.
Okay, so now we've got our package volume here is being calculated as
the product of those three dimensions.
And we may as well say that the package cost is the
base cost, right, right now.
We could start with that.
It's always going to be $2.50 plus perhaps some surcharge.
Okay, so that's where we're at.
So far so good.
Now we need to do these checks for the package volume
exceeding those thresholds.
What we want to do first is, we want to check the the tier 2
threshold because it's the highest.
So what we want to do here is we want to say if the package volume is
greater than the tier 2 threshold.
If that's the case, then the package cost plus equals, we're
incrementing that by the package cost * the tier 2 surcharge.
Remember, we're incrementing the left-hand side by the right-hand side.
And we could put a debug statement in here.
Again, it's very common to do that, just saying adding tier 2 surcharge.
We can get rid of these when we ship the code.
But it's nice to have them in here.
Soon I'll teach you how to use a debugger,
which makes this even easier.
Okay, so if this is not the case, then we can say else if.
The package volume exceeds the tier 1 threshold.
And if that's the case, we can basically just copy this.
And I'll paste it in here.
In this case, the package cost is the plus equals package cost * the tier
1 surcharge, and we can just change that statement right here to do that.
Okay, so we're really done.
What we need to do now is say cout the volume, we'll give
the user some information.
The volume of your package is and we'll display the
volume, package volume.
We have an endline here.
And then we'll just tell them how much it's going to cost
that's really what they want.
Your package will cost, we'll put a dollar sign here.
And I believe it was package cost.
There it is, to ship and that should do it.
Let me clean up some of this white space, and we'll run this.
And then we'll do one more little thing before we go.
And let's see tier one surcharge I spelled it wrong.
I should need an underscore right there.
And that way you can see some of these errors. Perfect.
So let's say 5 5 and 5.
I'm adding a tier 1 surcharge because the volume is greater than 100.
You can see here's 125.
So the package will cost 275 to ship.
Let's make it a little bit bigger the package.
Let's say it's 8 x 8 x 8.
In this case, I'm adding a tier 2 surcharge.
The package volume is 512.
And it's gonna cost $3.12 to ship.
Okay, Now you'll notice this 3.125 displays.
I'll show you how to set that a little bit later in the course.
But for now, I'll just show you how to do it really quickly.
If you come up to the includes and you include io manip, that's
the input output manipulators.
And then right before you start printing the dollar amounts or
actually anyway you can put this anywhere you could just say cout
fixed followed by set precision to two decimals after the package.
This is that just prints dollars nicely so.
If we run this.
Again, let's try that again, we'll do 8, 7 and 9.
You can see it prints it out nicely, $3.12.
And again, I'll talk about those manipulators a little bit later on.
It just helps us look at our code a little nicer.
All right, so there you go.
There's some logic for shipping this package.
We've got an if else statement.
We've got an if else if statement.
And hopefully, this logic makes sense.
Again, build these programs, one step at a time testing as you go.