Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Nested Conditional Statements3:50 with Alena Holligan
Sometimes we need a conditional that evaluates more than one expression. For example: maybe I want to match a range of numbers like 10 through 1000. We need to do two tests -- is the number greater than or equal to 10 AND is the number less than or equal to 1000? In this video we'll look at how to nest conditional statements.
Have you ever found yourself using the same word to express yourself in conversation? For example:
'Programming is kewl'
'Bungee jumping is kewl'
'My vacation was kewl'
While there is nothing technically WRONG with these sentences, the concept being conveyed is not clear. You could be more descriptive by saying:
'Programming is stimulating'
'Bungee jumping is thrilling'
'My vacation was relaxing'
These sentences give a much clearer description of the concept being conveyed. In the same way, you can get comfortable using equality conditionals and force your code to fit into that syntax, even though you may actually mean identical or even NOT equal. Try to use the expression that most clearly defines what your code should actually be doing.
Sometimes we need a conditional that evaluates more than one expression.
For example, maybe I want to match a range of numbers like ten through 1,000.
We need to do two tests.
Is the number greater than or equal to ten?
And is the number less than or equal to 1,000?
One way to do that is nesting two conditional statements.
Let's take a look at how we would do that.
Since we've already looked at these examples,
let's comment out the rest of these conditionals.
Now, let's go to the end of our file and add a new line.
Let's create a new variable called num and
set it equal to 100 then we'll add our first IF statement.
IF num is greater than or equal to 10, then
we can add a second nested conditional inside this conditional
If num is less than or
equal to 1000, now we can add the display statement.
Your number is within the range.
Close out our if statements and add an else.
Your number is NOT within the range.
Now, when we run our script, we see that your number is within the range
because 100 is greater than 10 and less than 1,000.
But what if we change num = 1?
And run our script.
We see that your number is not within the range.
Now what happens if we change this to 10000.
Now when we run our script,
we get no output because it met our first condition but not our second condition.
So we had not told our script to display anything.
We could add a third display.
Else echo your
number is greater than 100.
Not within the range.
If we want to show a separate message based on if the number was greater than or
less than the range this would make sense.
We could also change our else down here to say your number
is less than 10 not within the range.
So now when we run our script,
we see your number is greater than one thousand not within the range,
and if we change our number back to one And
run the script, we see that your number is less than 10, not within the range.
If we want to show a single message when the number is outside the range,
We've created duplicate code, that will make things harder to update,
if we ever want to change this message.
Also, nested if statements can get messy quickly,
especially if we start writing nested statements, that are several levels deep.
We have to remember, what conditions actually brought us to this point.
And as we continue to indent,
we have to start considering how our code will wrap to the next line.
In the next video,
I'll show you how we can use logical operators to solve these issues.
You need to sign up for Treehouse in order to download course files.Sign up