Gary Marshall wrote:
The result of this code is "false". OK, so please tell me how the compiler analyzes line 1. It obviously did not execute "b1 = i1" first
...
And, even though it would cause a compiler error, why did it not try to execute "b1 = i1" first?
"if (b1 ="
At this point, we have the boolean variable b1 and a symbol (the equals sign) indicating that something will be assigned as the value of that variable. But what will be assigned?
Everything to the right of the '=' will be evaluated (not executed[i]) and assigned to b1.
So then, what's to the right of "b1 =" ? An expression of the form
"i1 == i2"
This expression is a claim that the value of i1 is the same as the value of i2 (on the relevant definition of "same as"). That claim is either true or false. Since it's false, "false" is the value assigned to b1.
The compiler doesn't choke on "b1 = i1" because of guarantees in the language spec about the order of evaluation. Evaluation is from left to right. All operands will be evaluated before an operation on them is evaluated. In case there are multiple operators, the leftmost value will be defined and retained before whatever lies to the right. Here's the skinny:
link.