Here is the output you get in the Unity Console:
bool theBearMadeBigPottyInTheWoods = true;
A bool
variable is declared and assigned the value of true
.
int temperature = 40;
An int
variable is declared and assigned the value 40
.
if(temperature >= 35 && theBearMadeBigPottyInTheWoods)
An if
statement to test if both conditions are true
.
The first test is checking if the temperature
is greater then, or equal to, 35
.
The value stored in temperature
is 40
, so this condition is true.
The value stored in theBearMadeBigPottyInTheWoods
is true. Therefore the first condition and the second condition are true, so the code block executes.
if(temperature >= 35 || theBearMadeBigPottyInTheWoods)
An if
statement to test if either of the conditions are true.
We already know that both the conditions are true, and either the first condition or the second condition needs to be true. Therefore the code block will execute.
Try changing temperature
to a lower value such as 30
. Only one of the if
statements will be true:
The following is the analysis of code:
if(temperature >= 35 && theBearMadeBigPottyInTheWoods)
Only one of the conditions is now true, as 30 is not greater then, or equal to, 35.
Therefore the first condition is false. Since both conditions have to be true, the code block does not execute.
if(temperature >= 35 || theBearMadeBigPottyInTheWoods)
Only one of the conditions is now true.
30 is not greater then or equal to 35, therefore the first condition is false.
The second condition is true.
Since only one of the two conditions has to be true, doesn't make any difference which one, the code block executes.
3.133.123.126