Reputation: 5191
I was solving the C++ Multiple choice questions. I am not able to understand the output for the following code::
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
x=y=z=1;
z=++x || ++y && ++z;
cout<<x<<" "<<y<<" "<<z<<endl;
system("pause");
return 0;
}
I am solving this question in following way:: Precedence order ::
Precedence "++" greaterthan Precedence "&&" greaterthan Precedence "||"
Also , the Associativity of unary++ is "Right to left" . So
z=(++x)||(++y) && (2)
z=(++x)||(2)&& (2)
z=(2)||(2)&&(2)
z=(2)|| 1 //As 2 && 2 is 1(true)
z=1 // As 2 || 1 is 1(true)
So as per me ,the correct output should be x=2,y=2 and z=1.
But When i ran this code in my compiler,the compiler output is x=2,y=1,z=1.
Why i am getting such output and where i am making mistake?
Thanks!
Upvotes: 0
Views: 317
Reputation: 198
Keep in mind that the compiler is going to execute the whole logical expression only if it NEEDS to. Otherwise, it tries to get away with as little work as possible.
For example, if you use the && operator, it is necessary for both expressions to be evaluated. However, if you use the || operator (which you have), if the first expression on the left hand side is true, the right hand side is not executed. In your example, ++x gives 2, which is evaluated to a boolean TRUE. End of story for the compiler since once true, an OR statement will never revert to FALSE. This concept is called 'short-circuiting' by the compiler. That is why you got the output that you described.
Upvotes: 1
Reputation: 25153
Logical OR Operator requires if any of the two operands is non zero then then condition becomes true. In the expression A||B
, either A
or B
is non zero then (A || B)
will br true or equal to 1
.
So ++y
and ++z
will be ignored by the compiler because value of ++x
is 1
.
Upvotes: 1
Reputation: 792867
Operator precedence tells you how to group expressions; it doesn't tell you in which order they are executed.
||
and &&
are special in that the first operand is always evaluated first and the second operand (including all sub-expressions) is only evaluated if it is required to determine the value of the expression.
For ||
, if the first operand evaluates to true
the second operand is not evaluated because the result of the logical-or will always be true.
Similarly, the second operand of &&
will not be evaluated if the first operand evaluates to false as the logical-and must be false in this case.
In the expression z=++x || ++y && ++z
, the grammar rules specify a grouping:
z = ((++x) || ((++y) && (++z)));
In the sub-expression (++x) || ((++y) && (++z))
, as (++x)
evaluates to true
(as 2 is non-zero), the second operator ((++y) && (++z))
is never evaluted. x
becomes 2, y
is unchanged and z
is assigned 1
(true
converted to an integer).
Upvotes: 3
Reputation: 43882
The &&
and ||
operators are short-circuiting. They evaluate the left side, and then evaluate the right side only if necessary to determine the value.
Therefore, if the left side of ||
is true the right side is not evaluated, and if the left side of &&
is false the right side is not evaluated.
In your example, since ++x
is true (2
), the right side of the ||
is not evaluated.
Upvotes: 3