In this video, we'll learn about the if else statement.
Adding An else section to an if statement is the obvious next step to
improving control flow in a program.
Many times, we want to perform different actions depending on whether
the condition is true or false.
Again, the syntax for the if else statement is very simple.
We have the same syntax as for the if section as in a simple if statement.
But we add the else keyword and the statement we want to execute
when the condition is false.
So in this case, if the control expression is true
we execute statement1.
And if it's false, we execute statement2.
Notice the indentation.
I can't stress enough how important good indentation and style is.
By simply looking at the construct, we can easily see both sections
because it's indented properly.
In this slide, you can see example code and the flow chart that
visually shows the flow of control.
The condition num greater than 10 is evaluated.
If that's true, we increment num.
If it's false, we assign 10 to num.
In both cases, we then continue execution to the next statement.
Let's see a few more examples.
In the first statement, we check if num is greater than 10.
If this is true, we display num is greater than 10.
If it's false, we display num is not greater than 10.
In the second example, we check if health is less than
100 and heal player is true.
If both these conditions evaluate to true, then we
assign 100 to the player health.
Otherwise, we simply increment the player health.
Here we could see sample code along with a flowchart that
visually shows the flow of control.
Notice that in this example, we're using two block statements
in the if else statement.
So if num is greater than 10 is true, we execute both
statements in the block.
We increment num, and we display increment.
If num is not greater than 10, then we assign 10 to num and display reset.
Regardless of which block statement executes, we continue processing.
Finally, let's see another example where we group if else if statements.
In many cases, we can group if else statements together.
Here's a common example.
Suppose I want to display a student's letter grade
given their score on an exam.
In this case, we can check if their score is greater than 90.
If it is, we display an a., and we're done.
No other conditions will be checked.
So we continue to the last output statement and display done.
Suppose the student scored a 75.
First, we check to see if the score is greater than 90.
It isn't.
So we go to the next else if score greater than 80.
Again it's, not true.
Then we check score greater than 70.
Now it's true, so we display a c, and we're done.
No other conditions will be checked, so we continue to the last
output statement and display done.
Notice that the last else in the construct doesn't have
an if associated with it.
This is the catch all else statement.
If none of the previous if statements is true, then this l statement
will execute and f is displayed.
This is a very very common pattern that's often seen in programming.
It's efficient since no extra checks will be performed once
we know which condition is true.
We could obviously change the logic and start with score less than
60 and work our way up to an a.
Let's head over to the IDE and see some examples in live code.
I'm in the CodeLite IDE.
I'm in the section 9 workspace in the if else statement project.
This one's really really simple.
The user is going to enter a number, and we're going to store it in num.
And we're going to compare the number to a target.
In this case, I just assigned 10 to the target.
So we'll ask them enter a number, and I'll compare it to 10 in this case.
Okay, we get the number from the user.
And then we've got a real simple if statement.
If the number is greater than or equal to the target, we're just
going to display this line, and then we're going to display the
number they entered is greater than or equal to the target.
That's it.
We're also going to calculate the difference between the number
and the target and display the difference.
So in this case, in this piece right here this is the
if statement right there.
That's the if section.
That's the true section.
We're going to say yeah the number you entered is greater
than or equal to the target.
It's so far away from the -- so much greater than the target.
That's it.
Now if the number they enter is not greater than or equal to the
target, then the else part fires.
And in which case we're still going to print a line, and then we're
just going to say the number you entered is less than the target.
What's the difference between target and number, and we're going to
display that difference as well.
Okay, so really really straightforward program.
Let's compile and run this.
So in this case let's enter a number and I'll compare it to 10.
Let's say I enter 10.
It says 10 is greater than or equal to 10, which it is and 10 is 0
greater than 10 because it is 10.
Perfect.
So let's run this a couple more times.
Let's put in 100, and we get 100 is greater than or equal to 10.
100 is 90 greater than 10.
Right, and let's put in let's negative 10.
In this case, negative 10 is less than 10.
And negative 10 is 20 less than 10, which is again correct.
So you can see real simple program adding an else section to the if
statement is really, really simple and actually it's super powerful because
it gives you you know the ability to have a binary decision and execute
code on both sides of that decision.