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. 0:00 For example, maybe I want to match a range of numbers like ten through 1,000. 0:04 We need to do two tests. 0:10 Is the number greater than or equal to ten? 0:11 And is the number less than or equal to 1,000? 0:14 One way to do that is nesting two conditional statements. 0:19 Let's take a look at how we would do that. 0:23 Since we've already looked at these examples, 0:26 let's comment out the rest of these conditionals. 0:28 Now, let's go to the end of our file and add a new line. 0:34 Let's create a new variable called num and 0:38 set it equal to 100 then we'll add our first IF statement. 0:42 IF num is greater than or equal to 10, then 0:47 we can add a second nested conditional inside this conditional 0:56 If num is less than or 1:01 equal to 1000, now we can add the display statement. 1:04 Your number is within the range. 1:15 Close out our if statements and add an else. 1:22 Your number is NOT within the range. 1:30 Now, when we run our script, we see that your number is within the range 1:38 because 100 is greater than 10 and less than 1,000. 1:43 But what if we change num = 1? 1:47 And run our script. 1:54 We see that your number is not within the range. 1:58 Now what happens if we change this to 10000. 2:01 Now when we run our script, 2:07 we get no output because it met our first condition but not our second condition. 2:09 So we had not told our script to display anything. 2:15 We could add a third display. 2:18 Else echo your 2:20 number is greater than 100. 2:25 Not within the range. 2:30 If we want to show a separate message based on if the number was greater than or 2:37 less than the range this would make sense. 2:42 We could also change our else down here to say your number 2:45 is less than 10 not within the range. 2:49 So now when we run our script, 2:55 we see your number is greater than one thousand not within the range, 2:58 and if we change our number back to one And 3:03 run the script, we see that your number is less than 10, not within the range. 3:08 If we want to show a single message when the number is outside the range, 3:15 We've created duplicate code, that will make things harder to update, 3:19 if we ever want to change this message. 3:23 Also, nested if statements can get messy quickly, 3:26 especially if we start writing nested statements, that are several levels deep. 3:29 We have to remember, what conditions actually brought us to this point. 3:34 And as we continue to indent, 3:38 we have to start considering how our code will wrap to the next line. 3:40 In the next video, 3:45 I'll show you how we can use logical operators to solve these issues. 3:46
You need to sign up for Treehouse in order to download course files.Sign up