- In this video, we're going to keep
discussing the if-else statements.
Now we're going to make them even more complex
by using nested if.
What do we mean by that?
Logic can become at times very complex,
demanding very intricate if-based decisions.
For example, if(currentSpeed < maxSpeed)
then I want to display the message
"You are driving at legal speed."
Else, now I have another decision to make.
If(currentSpeed == maxSpeed)
then print "Be careful, slow down."
Else, that's yet another choice,
"Slow down now!"
So what is the logic of this?
First, the speed is less than in maximum speed,
which is fine, now I have an alternative.
It is not less than maximum speed.
In that case it could be
either equal to the maximum speed
or if it's neither, less than
and it's not equal, the third choice has to be
that it's bigger than, in which case
I'm displaying the message "Slow down now."
I don't need to test another if
because it's the only available option.
Let's see a similar example in code,
and we'll see how this all works in C.
We're going to stay within the same
framework of mind of traffic and speed.
And let's write the little program
that checks the color of a light at a traffic stop.
We will ask the user to enter R for red,
Y for yellow, and G for green.
And we will do that in a function
called checkTrafficLight call from main.
Note that this is the only function that is call from main.
This function in turn calls another function, processColor.
That's the one that would enter by the user.
Let's look at processColor that implements
most of the logic of this program.
First I'm testing whether the user has entered R.
If the user has entered R,
I'm interpreting this as the traffic light is red.
Else, meaning it was not R,
so let's see what other options do I have.
if (c == 'y'),
I'm interpreting this as being yellow.
Else, I'm continue to test.
If it wasn't R and it wasn't Y, maybe it's a G.
So if (c == 'g'),
then I'm printing ("The traffic light is GREEN").
Else, meaning it was not R,
it was not Y, it was not G, it must be something
that is an illegal input to this program,
in which case I'm displaying the message
"unrecognized color."
I hinted to the user to enter only
those three options, red, yellow, and green.
Otherwise, it will display an error message.
Notice one more thing, one of the advantages
of the nested ifs approach is that
as soon as the correct option or choice
has been made or identified,
there is no need to check the other options.
For example, in the beginning, if the user typed R,
the entire block of code starting
in line 39 through 46 is ignored.
There's no need to go through it.
However, if it is not R, we will
only test then line 40.
If line 40 is result, meaning the user here's typed y,
there is no reason to perform
the other sections in line 42 and so on.
So only in the worst case scenario
where the user has typed a wrong choice,
the program has to go through all the options
to finally detect that the user had made a wrong
or an unrecognized option as this program is written.
Let's now test this program and see
how this all if block works.
Okay, so first I'm asked to enter a color,
R, yellow, or green, let's type Y for yellow.
The traffic light is yellow, great.
Let's run it again, going to type F for green.
Traffic light is green.
Now I am going to type some unrecognized letter, D.
Got the message, unrecognized color,
which means the logic works as planned.
You will see in the upcoming videos
how we can make this even more sophisticated,
more efficient, and what programmers like to call
a more elegant solution.