In: Computer Science
Recall the is_hour(str) and is_minute(str) functions return True if the str contains valid hour and minute respectively. That is, is_hour("03:65") would return True and is_minute("03:65") would return False.
For each of the following expression, indicate whether short-circuit evaluation would occur. That is, the function call on the LHS of the logic operator would not actually be evaluated.
Expression | Short-circuit evaluation | Expression | Short-circuit evaluation |
is_hour("03:65") and is_minute("03:65") | --YesNo | is_hour("03:65") or is_minute("03:65" | --YesNo |
is_minute("03:65") and is_hour("03:65") | --YesNo | is_minute("03:65") and is_hour("03:65") | --YesNo |
The answer to the above question is as follows-
Short-circuit evaluation - It means that while evaluating any boolean, OR / AND expression, we stop as soon as we encounter a satisfaction or negation part.
Expression 1-
is_hour("03:65") and is_minute("03:65")
Short-circuit evaluation - NO
is_hour("03:65") returns True and AND condition requires both to be true to result in true .So second part,i.e,is_minute("03:65") is also evaluated which results in false. So there is not short circuit in this expression.
Expression 2-
is_minute("03:65") and is_hour("03:65")
Short-circuit evaluation - YES
is_minute("03:65") returns False and since, here we have an AND condition, first part being false results in false. So no need to check the second part. So there is short circuit evaluation.
Expression 3-
is_hour("03:65") or is_minute("03:65")
Short-circuit evaluation - YES
is_hour("03:65") returns True and this is an OR condition, it only needs one true part. So no need to check for second part and there is short circuit evaluation here.
Expression 4-
is_minute("03:65") or is_hour("03:65")
Short-circuit evaluation - NO
is_minute("03:65") returns False and since OR condition requires at
least one true to result to true, it check the next part if its
true. Then is_hour("03:65") part is checked. So there is no short
circuit evaluation.