From the information online I have gathered that order of evaluation is evaluating the left side of the operand before the right. However when looking online this is also similar to short circuiting, as that is also evaluating the left before the right. Below my code represents short circuiting however would like it to show order of evaluation. Would anyone know what would need to be changed within the code below to show the code in terms of order of evaluation as opposed to what its currently being shown as a short circuit when running within my compiler. Compiler uses Minijava.
class ImpFact{
public static void main(String[] a){
System.out.println(new Fac().ComputeFac(10));
}
}
class Fac {
public int ComputeFac(int num){
boolean a;
boolean b;
int result;
a = false;
b = false;
if (a || b) {
result = 3;
}
else {
result = 7;
}
return result;
}
}
I'm not sure that I understand what you are expecting as a response to the question.
But it's easy to characterise the difference between short-circuit evaluation and strict order of evaluation.
Consider the statement:
b = f(a) + g(a)
In a language which guarantees strict left-to-right evaluation order (such as Java), then function f
will be called before the function g
, but *both functions will always be called.
On the other hand, with the expression
b = f(a) || g(a)
f
will be called first, but it is quite possible that g
will not be called. The evaluation can stop after f
returns, if it returns a true value.
There are also languages, like C, that don't guarantee left-to-right evaluation. In those languages, when f(a) + g(a)
is evaluated, it is possible that g
is called before f
. But short-circuit evaluation (with boolean operators) works the same because short-circuit evaluation must be strictly ordered.