Next: , Up: Expression Evaluation


2.13.1 Evaluation Order

Expressions are evaluated left to right and always in order. There are thus differences from C expression evaluation order.

Assuming a `=' attribution operator with C semantics and two variables, `x' and `y', the `x = y = 0' expression will not pass, for the evaluation order is left to right and thus the second `=' operator attempts to set its left operand (`x = y') to the value of its right operand (`0'). The `x = (y = 0)' expression will do fine, though.

Differences from C expression evaluation are also due to always in order rule. Thus a subexpression occuring before a second subexpression is always evaluated before the same second subexpression. Function arguments are not evaluated before the expression.

`(x = 0) + cos(x)' expression evaluates to 1, no matter the value of assumed numeric variable `x' before expression evaluation and assuming a cosine function called `cos'. A C compiler (like the one I am using now) still evaluates the expression to 1, yet it claims that `operation on `x' may be undefined'.

Assuming a `real modf(real, real &)' function with C modf semantics and two real variables, `e' and `f', valued 2.5 and 4, respectively, the expression `f + 0 * modf(e, f)' evaluates to 4 (`f' will be set to 2). The C expression f + 0 * modf(e, &f), where both e and f are doubles, evaluates to 2 (and sets f to 2) (at least the compiler I am using does so).