What are the possible ways the following expression is actually evaluated?

38 views Asked by At

Note - Assume the code is in JAVA

Suppose I have a function -

public boolean Helper(){
boolean a = ...;
boolean b = ...;
boolean c = ...;
boolean d = ...;
return a || b && c || d;
}

I want to understand what are the possible ways in which the expression is actually evaluated. I am not talking about the logic table where we fill up ones and zeroes. For example - To my understanding, if a is true, it won't matter what values the rest of the variables hold.( As there will be short circuit evaluation) Can someone please help me in listing down all such conclusions? This boolean expression is very confusing to me

1

There are 1 answers

0
rzwitserloot On

All you need to know is three things:

  1. Precedence rules. That is evaluated either as (a || b) && (c || d), or as a || (b && c) || d or as ((a || b) && c) || d. A simple matter of searching the web for 'java operators precedence' will get you your answer.

  2. Awareness that in java, the doubled up versions of the or and and operator (|| and &&, being used here) 'short circuit'. a && b will not eval b if a is false, as the result is already known.

  3. basic logic tables.

All this stuff is trivially found. I think the homework exercise partly involves you actually looking all this up :)