According to my limited knowledge logical operators have left-right order of evaluation. By that rule it should evaluate x && y which is 1 and then it should evaluate == 1 in the if statement. But that is what not happening. Could anyone help me with this.
int main()
{
int x = 1, y = 2;
if (x && y == 1)
printf("true\n");
else
printf("false\n");
}
The order of operations is different than what you think.
Your expression is equivalent to
x && (y==1)
which is false in your case.
try this.
because of https://en.cppreference.com/w/c/language/operator_precedence
int main()
{
int x = 1, y = 2;
if ((x && y) == 1)
printf("true\n");
else
printf("false\n");
}
Try not to think about order of evaluation too much. It will often confuse you. The more important things to focus on is how the operators are grouped together and what the expression means.
The && operator has relatively low precedence. Almost any time you use it, it will have the form
if( condition_1 && condition_b )
and the interpretation is the obvious: "if condition_a is true and condition_b is true".
In your case, condition_a is just x, and condition_b is y == 1. So the interpretation is "if x is true and y equals 1".
What does it mean for x to be true? Simply that it's not zero. So we could expand this further: it's as if you had written
if ( x != 0 && y == 1 )
and the interpretation is "if x is not equal to 0 and y is equal to 1".
Remember, precedence says how the operators are grouped together. In your case, it's as if you had written
if ( x && ( y == 1 ) )
The == 1 part goes with the y. == binds more tightly than &&.
In terms of "order of evaluation", you can say that, yes, the compiler emits code that figures out whether x is true, and code that figures out whether y is equal to 1, before it uses && to determine whether they're both true. In the case of &&, we also know that it will decide whether x is true before it decides whether y is equal to 1. (But this is a rather special property of the && and || operators. It does not apply to most of the rest of the more ordinary operators like + and /.)
Because y == 1 is false as y = 2, written before if statement.
It does not evaluate they way you think. It evaluates x and then y == 1 and then &&, which is in your case in 1 && 0 and you have false result.
Here
if (x && y == 1)
above statement is equivalent to
if(x && (y==1) ) /* y==1 performed first, which is false */
As you can see here https://en.cppreference.com/w/cpp/language/operator_precedence
You want to get true as output, then first perform x&&y by keeping ().
if ( (x && y) == 1) { /* now () has higher precedence, so it perform x&&y first which results in true(1) and then 1==1 results in true */
}
else {
}
This is because of operator precedence.
Because of operator precedence the expression if (x && y == 1) results in if (x && (y == 1)).
So, the whole expression results to false because y==1 is false.
Use parenthesis and change it to if ((x && y) == 1) to get desired result.
Related
First this is the question I'm working on:
Evaluate each of the following expressions in C:
int x=1, y=7, z=0;
char a='m';
1) a ? y-x : x–y
2) x = 5 ? (y = z) : (z = y)
I understand the rest of the questions but number (1) confuses me... isn't it supposed to be a logical expression?
I mean 'm' cannot be true nor false; how can I answer this question? Is it simply "Error"? Or is there something I missed?
For number (2) the statement (z=y) should execute which changes z to 7, but isn't it supposed to be x == 5 and I tried it on a terminal and it changes both x and y to 0.
What am I missing?
In C ANY numeric type can be evaluated as a bool, and for such types, any non-zero value is 'true'. So since the character 'm' is non-zero (only '\0' is zero), it is "true"
Precedence -- All operators in C have precedence, and ?:, while lower than most, is higher than assignment operators. So this expression is equivalent to:
x = (5 ? (y = z) : (z = y))
I know what is short circuit evaluation in C.
a && b (operand b is not checked if a = 0)
a || b (operand b is not checked if a = non zero)
But I am stuck at this question
int x = 0;
if (5 || 2 && ++x)
printf("%d", x);
This outputs 0.
My first thinking goes as follows:
According to precedence table , precedence is ++, &&, || (descending order)
++x: evaluated.x becomes 1.
2 && ++x evaluated. Both operands are evaluated.
|| is evaluated.
But according to this, 1 should be printed, not 0.
My second thinking goes as this:
5 || anything
anything is not evaluated because of short circuit evaluation, so no precedence comes into play here.
The expression 5 || 2 && ++x is equivalent to 5 || (2 && ++x) due to operator precedence.
The run time evaluates the expression 5 || 2 && ++x from left to right.
As we know in OR if first condition is true it will not check the second condition.
So here 5 evaluated as true and so (2 && ++x) will not be performed.
That's why x will remain 0 here.
Correct. The expression is short circuited. You can test it with this.
if(5 || ++x) {
printf("%d\n",x);
}
I was looking into a code snippet and saw below statement. How will below statement evaluated?
x= 5|(high == 1 ? y : high == 0 ? z:0);
The expression
x= 5|(high == 1 ? y : high == 0 ? z:0);
is evaluated as
x= 5|( high == 1 ? y : (high == 0 ? z:0) );
It has similar effect as that of
if(high == 1)
x = 5|y;
else if(high == 0)
x = 5|z;
else
x = 5|0;
Its like
if(high == 1)
y;
else if(high == 0)
z;
else
0;
From The C99 standard, section 6.5.15.4:
The first operand is evaluated; there is a sequence point after its
evaluation. The second operand is evaluated only if the first
compares unequal to 0; the third operand is evaluated only if the
first compares equal to 0; the result is the value of the second or
third operand (which ever is evaluated), converted to the type
described below. If an attempt is made to modify the result of a conditional operator
or to access it after the next sequence point, the behavior is
undefined.
Because operands are evaluated left to right, the second instance of the ternary operator (all three parts) becomes the expression in the third part of the first ternary operator.
So this:
high == 1 ? y : high == 0 ? z:0
is equivalent to this:
(high == 1) ? y : ((high == 0) ? z:0)
I understand a condition is supposed to be for example y==3, but y-3 is totally confusing to me. I can't understand this code. If y-3 is true it prints 1. How can y-3 be true or false?
The expression y - 3 results in a value which can be tested for truthness. In C 0 is false and any non-zero value is true. Saying if (y - 3) is essentially saying if (y - 3 != 0) or even more succinctly if (y != 3)
In C, everything what's 0 is false and everything what's non-zero is true.
When tested directly into a boolean context, integers can be evaluated like this.
0 is equivalent to false and all the other values are considered true.
In your code y - 3 will be equivalent to false only if y - 3 == 0, that is if y is 3.
A better style would be to write:
if (y - 3 != 0)
Or even:
if (y != 3)
if (E)
where E is an expression is equivalent to
if (E != 0)
In an if statement (or any statement with a condition), the condition is considered true if it's unequal to zero, false otherwise.
So this:
if (y - 3)
is equivalent to this:
if ((y -3 ) != 0)
which can (and should!) be re-written as:
if (y != 3)
The == operator, for example, yields an int value of either 1 or 0.
C does have a built-in Boolean type (called _Bool, introduced by the 1999 standard), but it didn't always exist, and it's not used in conditions. Of course you can use a _Bool value as a condition, but it's still tested (at least conceptually) for inequality to zero.
First, the expression 'y-3' will be evaluated. If the result is 0 (a.k.a, the value of y is 3), then the expression will evaluate to false, and the value "2" will be printed. If y is equal to any value other then 3, the condition will evaluate to true, and the value "1" will be printed
This would mean exactly the same
if ((y - 3) != 0)
printf("1");
else
printf("2");
So that's going to be true if it's not zero.
Here is my problem: x is 11, y is 6 and z is 1.
what is the value of w?
w = x == y || x != y && z > x;
I dont understand how to decide whether I should evaluate the || or the && first.
&& has higher precedence than ||. What else does one need to know?
x == y || x != y && z > x;
x != y evaluates to true, and z>x evaluates to false so x != y && z > x evaluates to false. So we have:
x == y || false;
Now since x==y is false we get:
false || false;
However order of evaluation does not depend on precedence if that is where you were confused. For a deeper understanding about what i said please read the answer here to my question by Jerry Coffin.
Operator Precedence vs Order of Evaluation
That would be evaluated like this w = (x == y) || (x != y && z > x) wikipedia has the full order for all operations http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages
Take a look at http://www.isthe.com/chongo/tech/comp/c/c-precedence.html for the order of precedence. In this case && has higher precedence than ||.
In case you are not sure, just use bracket. Putting a bracket is much more useful rather than having such a headache.