HP Forums
Short circuit logic - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Short circuit logic (/thread-14862.html)



Short circuit logic - mchris - 04-18-2020 08:17 AM

I found that I can replace IFs with short circuit logic. Example:

IF a>b THEN
do_something;
do_something_else;
END;

can be replaced by:

(a>b) AND (do_something; do_something_else);

as long as do_something and do_something_else are only calls to other functions or assignments. There must not be statements like IFs, RETURNs, BEGINs, loops... But if this is a really long line the Prime finds an error in it. Any thoughts?


RE: Short circuit logic - DrD - 04-18-2020 11:06 AM

My thoughts:

With tools, such as IF THEN ELSE END; and IFTE(); which are designed to accomplish this task, there is little to be gained by using the shortcut. Considering the programming effort overall, avoiding unusual syntax, (which might have unintended side effects directly, or in other releases), seems like a more dependable choice.


RE: Short circuit logic - Carlos295pz - 04-18-2020 11:35 AM

It is for the sake of simplification, in a logical operation when using "AND", if the first value is false it will obviate the following values โ€‹โ€‹of the same level, because in an "AND" operation, a false is enough for everything to be false.
This is a valid alternative:
Boolean_real AND Function_or_ (operation)

"Function" or "Operation" must return a real or integer number, because logical operators only accept those values.

(operation1; operation2; ...): This is a syntax error
Examples of argument error:
Boolean_real AND Complex --- bad
Boolean_real AND String --- bad
Boolean_real AND Unit --- bad
...


RE: Short circuit logic - Carlos295pz - 04-18-2020 11:39 AM

(04-18-2020 08:17 AM)mchris Wrote:  IF a>b THEN
do_something;
do_something_else;
END;

can be replaced by:

(a>b) AND (do_something; do_something_else);

In logical operation format it would be like this::

(a>b) AND (do_something) OR (do_something_else);

Knowing that do_something and do_something_else must return a real or integer otherwise an error occurs in execution. do_something must return a value other than 0

Example:
Code:
EXPORT LogicExpr(Boolean)
BEGIN
Boolean AND MSGBOX("True") OR MSGBOX("False") 
END;



RE: Short circuit logic - CyberAngel - 04-18-2020 07:52 PM

(04-18-2020 11:06 AM)DrD Wrote:  My thoughts:

With tools, such as IF THEN ELSE END; and IFTE(); which are designed to accomplish this task, there is little to be gained by using the shortcut. Considering the programming effort overall, avoiding unusual syntax, (which might have unintended side effects directly, or in other releases), seems like a more dependable choice.

Right!
IFTE is the way to go here!
โ€“ โ€“
VPN