Add lines 169-171 (short circuit in logical operators) (#27061)

This commit is contained in:
ishan-sriv
2019-04-06 17:22:54 +05:30
committed by The Coding Aviator
parent 177a014fdc
commit e72caaa534

View File

@ -189,6 +189,10 @@ Because the previous code was executed, it won't check the other else statements
Finally, does `n > 5` evaluate to true? The answer is no, because n *is* 5, and so it is not greater than 5. As a result, this code will not evaluate to true. In order to make this evaluate to true, the `>=` operator should be used.
Short-circuiting in logical operators: The AND and OR operators exhibit "short-circuit" operation. That is, the second expression is not evaluated if the result can be deduced solely by evaluating the first expression.
In an AND operator, if the first expression is evaluated to false, the result is false, even if the second expression is true. Similarly, in an OR operator, if the first expression is true, the second need not be evaluated as the result would be true.
## A Detail about C Comparisons
Earlier you read that the comparisons are checking if something is true or false, but that's really only half true. Remember that C is about being light and close to the hardware- in hardware, it's easy to check if something is 0, and anything else takes more work. Because of this, what the comparisons are really doing is checking if something is false, which is assigned the value of 0, or checking if it isn't false (any other value).