- Read Tutorial
- Watch Guide Video
The reason for that is because conditionals
allow you to have dynamic behavior in your application. This is also something that you can have an analogy in a real-life scenario. We deal with conditionals all day long.
Not even in regards to programming, but if you're driving down the street and you see that you come up to a red light, that is actually you running a conditional in your head where you say: "if the light is red then I'm going to stop."
Conditionals in programming work very similarly. Let's say that you're building out a program for a rental car company, and the rental car company says that if the driver's age is under 25 years old: they're not allowed to rent that car. That is a conditional.
Now, we're going to start off with those basic types of scenarios, and then we're also going to extend it into some more advanced kinds of concepts, such as compound conditionals. To take that rental car, for example, say that you have a policy where you can rent a car if you're 25 years through 80 years old.
Well, you can actually build a compound conditional that checks for both those scenarios. So it can make sure that a data point fits inside of a pre-determined area. At the end of the section we're going to extend our knowledge and we're going to talk about switch statements
. So we're going to spend the majority of our time working with if-else conditionals
.
Those are going to be what you're using probably about 95% of the time. There also is this concept in JavaScript called a switch statement
, and so we're going to examine how you can work with those. I'm also going to use an example from a real-life project that I personally built, so you can see when you'd want to use one type of conditional versus another.