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

#include  int 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!