Day 3: Logic
Lesson 2: The AND operator
And here's an expanded version where we check a range of temperatures
In this final example you can see that we've had to expand the middle if statement to do two checks temperature > 15 && temperature <= 28
. Let's break it down a bit:
First it will evaluate
temperature > 15
Then it will evaluate
temperature <= 28
Then the results of these will be evaluated with the
&&
operator. This is the 'AND' operator and will return true if the things on both sides of it are true. If they are both false or just one is true then it will return false.
So if the temperature is 22 then:
temperature > 15
will be truetemperature <= 28
will be truetrue
&&
true will be true
So the if statement will execute the code in between its { }
brackets
If we try with temperature = 2
then
temperature > 15
will be falsetemperature <= 28
will be truefalse
&&
true will be false
The if statement won't execute the code between it's { }
brackets because the result of the code in between it's ( )
brackets is false.