Decision making
Now that we know the different kinds of variables, we can start to use them in logical expressions. We’re going to write code that decides something.
If this is true, then do that
Try these lines. Use the ⇥ tab key to indent the ones with print actions.
temperatureCelsius = 21
if temperatureCelsius > 20
print "A T-shirt will do."
else
print "Bring a sweater."
» "A T-shirt will do."
That’s not too hard, right?
If the temperature is greater than 20° C, a T-shirt will do; else you’ll need a sweater.
You’ve already noticed that CoffeeScript uses indentation to define which lines are inside a block. If you want more actions to be executed when ‘temperature is greater than 20°’, you place them all on the same level.
if temperatureCelsius > 20
print "A T-shirt will do."
print "…and flip-flops."
Many other programming languages use {
curly brackets }
to encapsulate what’s in a block, but CoffeeScript, just like Python or Haml, uses indentation.
The same lines in e.g., JavaScript would look something like this:
if (temperatureCelsius > 20) { console.log("A T-shirt will do."); } else { console.log("Bring a sweater."); }
But JavaScript developers will write with indentation anyway to keep it more legible.
if (temperatureCelsius > 20) {
console.log("A T-shirt will do.");
} else {
console.log("Bring a sweater.");
}
Let’s continue. We can have more than just two outcomes. Next to if
and else
, there’s also else if
.
temperatureCelsius = 0
if temperatureCelsius > 20
print "A T-shirt will do."
else if temperatureCelsius <= 20
print "Bring a sweater."
else if temperatureCelsius <= 10
print "Definitely a sweater,"
print "but bring a coat as well."
else if temperatureCelsius <= 0
print "It’s literally freezing!"
print "Take that winter coat!"
» "Bring a sweater."
By the way, as you must have figured out:
> | ‘is greater than’ |
>= | ‘is greater than or equal’ |
< | ‘is less than’ |
<= | ‘is less than or equal’ |
But we have an error in our output—a bug.
It’s freezing, but the output is still “Bring a sweater.”
…and not “It’s literally freezing! Take that winter coat!” which it should be because the temperature is ‘less than or equal’ to zero.
When dealing with if-then-else statements you have to remember that they always work from top to bottom. If one of the conditions is true, then it will stop right there.
Let’s walk through our code to try to find the bug.
- First condition:
Is the temperature greater than 20?
Nope, so it continues. - Second condition:
Is the temperature less than or equal to 20?
Yes! So it stops here and does the print action.
We found the problem. Let’s fix it now.
When checking a value, it’s essential to work from big to small, or from small to big. So we’ll start with checking for a high value— “Is the temperature greater than 20?”—and then work downward.
temperatureCelsius = 0
if temperatureCelsius > 20
print "A T-shirt will do."
else if temperatureCelsius > 10
print "Bring a sweater."
else if temperatureCelsius > 0
print "Definitely a sweater,"
print "but bring a coat as well."
else if temperatureCelsius > -10
print "It’s literally freezing!"
print "Take that winter coat!"
else # meaning: -10ºC or lower
print "Stay inside!"
print "Don’t you want a hot coffee?"
» "It’s literally freezing!"
» "Take that winter coat!"
Solved!
We can also check for other things besides bigger than or smaller than.
is | ‘is equal to’ |
isnt | ‘is not equal to’ |
thereIsSnow = yes
if thereIsSnow is yes
print "Don’t forget to bring the skis."
» "Don’t forget to bring the skis."
As you can see, you can omit the else
when you don’t need it.
personA = "John Doe"
personB = "Chuck Norris"
temperatureCelsius = -24
personGoingOutside = personB
if temperatureCelsius <= -20
if personGoingOutside isnt "Chuck Norris"
print "You ARE going outside?"
print "Are you absolutely sure?"
else # meaning: It is Mr. Norris
print "Nice T-shirt, Mister Norris."
else
print "Good day. Lovely weather, not?"
» "Nice T-shirt, Mister Norris."
Multiple levels of if
–else
statements, like in the example above, are perfectly possible. But there’s a more elegant solution for checking if two (or more) conditions are true: and
.
personA = "John Doe"
personB = "Chuck Norris"
temperatureCelsius = -24
personGoingOutside = personA
if temperatureCelsius <= -20 and personGoingOutside isnt "Chuck Norris"
print "You ARE going outside?"
print "Are you absolutely sure?"
else
print "Good day. Lovely weather, don’t you think?"
» "You ARE going outside?"
» "Are you absolutely sure?"
There’s also an or
operator, which helps you check whether any of the conditions are true.
personSteppingOnWater = "Jane Doe"
if personSteppingOnWater is "Jesus" or personSteppingOnWater is "Chuck Norris"
cancelGravity()
In this case, we are triggering a function; cancelGravity
is a variable that contains a function.
It’s a bit hard to stop gravity in CoffeeScript, though. We’ll start with simpler functions.