Just like logical operators, conditions also use true/false to make decisions. As you saw in the video, they do this with if/else statements.
If checks a condition. If the condition is true, it performs a specified action. If the condition is false, it performs a different action. This second action is specified by else. It looks a little something like this:
You can apply if/else statements to your everyday life. Imagine you’ve got a hankering for some fancy chocolates - you know, the kind that come in a box.
You’ve started to notice that you’re making a lot of trips to the store to buy chocolate, which isn’t the most efficient use of your time. That said, fancy chocolate is expensive, so you can’t buy too much of it at once. As you’re heading to the store, you can come up with a game plan for your chocolate purchases:
The same logic can be applied to all sorts of situations, like getting a new pet at your local animal shelter:
Greater than, less than, equal to
You may remember these from math class. You can safely say that the following examples are all true:
30 > 15
Thirty is greater than fifteen.5 < 10
Five is less than ten.3 = 3
Three is equal to three.
Still with me? You can evaluate these kinds of expressions as either true or false. You can also add them to if/else statements. Let's start with greater than:
What about a less than?
You could also do an equal than, but you get the idea. 😉
Summary
Use if/else statements for conditions.
You can compare elements using greater than, less than, and equal to.