The reason is simple: precedence. As you say, the order is:
- &&
- =
- and
Since && has precedence over =, the statement is evaluated like this:
if a = (f(2) && (b = f(4))) then
Which results in:
if a = (2 && 4) then
When x and y are integers, x && y returns y. Thus 2 && 4 results in a = 4.
For comparison's sake, the first one is evaluated like this:
if (a = f(2)) and (b = f(4)) then