How to resolve the algorithm Short-circuit evaluation step by step in the PL/I programming language
How to resolve the algorithm Short-circuit evaluation step by step in the PL/I programming language
Table of Contents
Problem Statement
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 (and): Then it would be best to not compute the value of b() if the value of a() is computed as false, as the value of x can then only ever be false. Similarly, if we needed to compute the disjunction (or): Then it would be best to not compute the value of b() if the value of a() is computed as true, as the value of y can then only ever be true. Some languages will stop further computation of boolean equations as soon as the result is known, so-called short-circuit evaluation of boolean expressions
Create two functions named a and b, that take and return the same boolean value. 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 b is only called when necessary: If the language does not have short-circuit evaluation, this might be achieved with nested if statements.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Short-circuit evaluation step by step in the PL/I programming language
Source code in the pl/i programming language
short_circuit_evaluation:
procedure options (main);
declare (true initial ('1'b), false initial ('0'b) ) bit (1);
declare (i, j, x, y) bit (1);
a: procedure (bv) returns (bit(1));
declare bv bit(1);
put ('Procedure ' || procedurename() || ' called.');
return (bv);
end a;
b: procedure (bv) returns (bit(1));
declare bv bit(1);
put ('Procedure ' || procedurename() || ' called.');
return (bv);
end b;
do i = true, false;
do j = true, false;
put skip(2) list ('Evaluating x with with ' || i || ' and with ' || j);
put skip;
if a(i) then
x = b(j);
else
x = false;
put skip data (x);
put skip(2) list ('Evaluating y with with ' || i || ' and with ' || j);
put skip;
if a(i) then
y = true;
else
y = b(j);
put skip data (y);
end;
end;
end short_circuit_evaluation;
You may also check:How to resolve the algorithm Here document step by step in the BQN programming language
You may also check:How to resolve the algorithm Vector step by step in the Sidef programming language
You may also check:How to resolve the algorithm String case step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Palindrome dates step by step in the RPL programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the XLISP programming language