Although Java has the rule that the left operand of every binary operator is eva
ID: 3701917 • Letter: A
Question
Although Java has the rule that the left operand of every binary operator 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 is the expression contains.
Explanation / Answer
Please find my answer.
Please let me know in case of any issue.
In the C++ Expression Like these can be a problem.Because They start to check Left First then Right
These Operators Like &&,|| just Evaluate only One side if not required.
1. Like in && Operator When First Condition is False then no needed to move to the 2nd condition this is smart work. But many times it may create a problem
int main()
{
int x=0;
int y=0;
if(x==0 && y++>0)
{
}
cout<<"x= "<<x<<" y="<<y;
return 0;
}
In this case Value of x and y are 0, 1
int main()
{
int x=1;
int y=0;
if(x==0 && y++>0)
{
}
cout<<"x= "<<x<<" y="<<y;
return 0;
}
But in this Case x and y values are 1 and 0 This is Draw Back sometimes
2. Like in || Operator When First Condition is True then no needed to move to the 2nd condition this is smart work. But many times it may create a problem
int main()
{
int x=1;
int y=0;
if(x==0 || y++>0)
{
}
cout<<"x= "<<x<<" y="<<y;
return 0;
}
In this case Value of x and y are 1, 1
int main()
{
int x=0;
int y=0;
if(x==0 && y++>0)
{
}
cout<<"x= "<<x<<" y="<<y;
return 0;
}
But in this Case x and y values are 0 and 0 This is Draw Back sometimes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.