Question: Although Java has the rule that the left operand is evaluated before the right operand, most languages give the compiler the freedom to choose which
Although Java has the rule that the left operand is evaluated before the right operand, most languages give the compiler the freedom to choose which operand is evaluated first. When expressions have side effects, the value of the expression can be different depending upon which order is used.
Give an example in C++ of an expression whose value depends upon the evaluation order. Show the orders that produce different values and the values they produce. Explain what side effect the expression contains.
students post that needs a response
#includeint x,y=0; int main() { std::cout << x++ + y++ << std::endl; return 0; }
In this example, the side effect is the variables incrementing. The output of the program will depend on whether the left (x++) or right (y++) side of the equation is evaluated first.
If the equation is evaluated left to right the output will be 0, with x=1 and y=1. If evaluating the right side first, the output will be 1, with y=1 and x=0.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
