[Math] Can these nested if-then-else be turned into a boolean formula

boolean-algebralogic

I have this logic statement:

    (A and x) or (B and y) or (not (A and B) and z)

The problem is that accessing A and B are rather expensive. Therefore I'd like to access them only once each. I can do this with an if-then-else construct:

if A then
  if x then
    true
  endif
else
  if B then
    if y then
      true
    endif
  else
    if z then
      true
    endif
  endif
endif

Is there a way to express this as a boolean expression? I have "and", "or" (both short-circuit) but no "xor".

I thought this would work:

X and (A or (Y and (B xor Z)))

But my test program (http://pastebin.com/EjURvpM4) shows it doesn't.

Best Answer

Basic idea is to then always check x, y, z before A and B. Given the short-circuiting of "and" and "or", here's an expression that will never evaluate A and B more than once:

(x or y) and z or (x and A) or (y and B) or z and not (A and B)

where the order of precedence from highest to lowest is: not, and, or.