Short-circuit evaluation
This page is the answer to the task Short-circuit evaluation in the Rosetta Code.
Description (from Rosetta Code)
Assume functions a and b return boolean values, and further, the execution of function b takes considerable resources without side effects, and is to be minimized.
If we needed to compute the conjunction (
Then it would be best to not compute the value of Similarly, if we needed to compute the disjunction (
Then it would be best to not compute the value of Some languages will stop further computation of boolean equations as soon as the result is known, so-called short-circuit evaluation of boolean expressions Task Create two functions named The functions should also print their name whenever they are called. Calculate and assign the values of the following equations to a variable in such a way that function
If the language does not have short-circuit evaluation, this might be achieved with nested if statements. |