One of the most interesting things a program can do is change how it responds depending on what is happening. Maybe it could respond to mouse movement, or to the current time, or to the weather? A key way to tell a program what to do in different situations is to use conditionals, expressed in code as if-statements.
if-statementselseelse ifWe all make decisions about what do based on information we have about the world. Something like this:
If there is freezing rain, then I’m going to stay at home.
This is made up of two key parts we can call the condition and the consequence:
if-statementsThere is a nice way of expressing conditionals in code called an if-statement. These if-statements let us tell the program what to do if a certain condition is true.
An if-statement expressing the above decision about freezing rain in program might look something like this:
// Check if there is freezing rain
// ("If there is freezing rain", the condition)
if (thereIsfreezingRain) {
// There is freezing rain, so stay at home
// ("then I'm going to stay at home", the consequence)
stayAtHome();
}
That is, we write:
if - the special word that starts a conditional(...) - parentheses that go around the conditionthereIsFreezingRain - the condition itself, inside the parentheses, needs to be something that will be either true or false (right now this one is just made up){ ... } - curly brackets around the consequencestayAtHome() - our consequence is to call the stayAtHome() functionSo, in general:
// Check if the condition is true
if (condition) {
// If it's true, the things inside these curly brackets
// will be executed
}
In a program a lot of the time our conditions are going to be based on numbers, so we’ll be checking things like:
(mouseX > 100)(mouseY < height)age equal to 33? (age === 33)?confidence greater than or equal to 90? (confidence >= 90)sadness less than or equal to 10? (sadness <= 10)As you can see, we use standard mathematical comparison operators (the fancy name for equals, less than, greater than, etc.) to compare numbers.
Note especially that we write three equals signs to check equality (===). Not one (which is what we use to assign values to variables) and not two (which works, but less consistently).
We will essentially always write comparisons that involve at least one variable because if there’s no variable involved then the condition cannot change and it’s pointless. You can check (5 > 2) as many times as you want, but it will always be true.
elseWhen we make decisions in life we often aren’t only thinking about what to do if something does happen (if the condition is true), we’re also thinking about what we’ll do if it doesn’t happen (that is, if the condition is false):
If there is freezing rain, then I’m going to stay at home. Otherwise, I’m going to the café.
Extending our program version, we would write this:
// Check if there is freezing rain
// ("If there is freezing rain", the condition)
if (thereIsFreezingRain) {
// There is freezing rain, so stay at home
// ("then I'm going to stay at home", the consequence)
stayAtHome();
}
else {
// There is no freezing rain, so go to the café
// ("Otherwise, I'm going to the café)
goToCafe();
}
So the else part allows us to tell the program what to do if the condition is false, which can be very useful! Notice there are no parentheses after the else because we don’t need to check anything. The else just happens if the condition is false.
else ifOne last thing! Sometimes we have more complicated decisions to make where there isn’t just one condition but several depending on each other. Such as:
If there is freezing rain, then I’m going to stay at home. Otherwise, if it’s 12 degrees or warmer, I’ll go for a walk. Otherwise, I’ll go to the café.
Extending our program version, we would write this:
// Check if there is freezing rain
// ("If there is freezing rain", the condition)
if (thereIsfreezingRain) {
// There is freezing rain, so stay at home
// ("then I'm going to stay at home", the consequence)
stayAtHome();
}
// There's no freezing rain, so check the temperature
// ("Otherwise, if it's 12 degrees or warmer")
else if (temperature >= 12) {
// There is no freezing rain, and the temperature is 12
// degrees or more, so go for a walk
// ("I'll go for a walk")
goForAWalk();
}
// Neither of the previous conditions were true
// ("Otherwise")
else {
// There is no freezing rain and the temperature is below
// 12, so go to the café
// ("I'm going to the café)
goToCafe();
}
So the else if part allows us to tell the program further conditions we want to react to, like the temperature.
Essentially we’re able to describe situations using our conditions and respond to them using our consequences. Based on the above code:
Conditionals in a programming language like JavaScript (that is, if-statements) allow us to express very dynamic and natural ideas in code: ideas about what we will do if some specific set of circumstances comes to pass. Responding to situations is part of what it means to be alive, and so conditionals in a very real sense help our programs to be alive!