Shruthi L
asked on
A && B || (C||D) how it works in c
A && B || (C||D) how does this expression works in c programming
() has highest priority
&& has higher priority then || so && will be performed first
First of All (C||D) will be performed
After that A && B will be performed
And at last the OR of the resultant of A && B and C || D will be performed
&& has higher priority then || so && will be performed first
First of All (C||D) will be performed
After that A && B will be performed
And at last the OR of the resultant of A && B and C || D will be performed
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@phoffric: Sorry, I think your last sentence is not correct, the left side of logical || or && operator is always evaluated first, so IMO ( A && B ) is evaluated first, only if its result is FALSE the ( C || D ) is evaluated.
You can even find it at http://en.cppreference.com/w/cpp/language/operator_logical: "Unlike the bitwise logic operators, these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first." (ok, this is C++, but I suspect it's different in C).
Best regards,
ZOPPO
You can even find it at http://en.cppreference.com/w/cpp/language/operator_logical: "Unlike the bitwise logic operators, these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first." (ok, this is C++, but I suspect it's different in C).
Best regards,
ZOPPO
@Zoppo,
You are absolutely right. I was thinking of arithmetic operations when I said that the order of operations could be either way. With logical operators, we have shortcuts so that if the the compiler, moving from left to right can conclude what the answer is by evaluating the left-most expressions, then it can avoid wasting time with the right-most expressions.
In other words, in both C and C++, if you have two logical expressions, linked like these:
A && B, then if A is FALSE, there is no need to evaluate B, since the result is always FALSE
A || B, then if A is TRUE, there is no need to evaluate B, since the result is always TRUE
My motivation in bringing up parenthesis as not an operator in the above example was to correct the statement:
>> First of All (C||D) will be performed
which is not true, ever.
Thanks for the catch.
You are absolutely right. I was thinking of arithmetic operations when I said that the order of operations could be either way. With logical operators, we have shortcuts so that if the the compiler, moving from left to right can conclude what the answer is by evaluating the left-most expressions, then it can avoid wasting time with the right-most expressions.
In other words, in both C and C++, if you have two logical expressions, linked like these:
A && B, then if A is FALSE, there is no need to evaluate B, since the result is always FALSE
A || B, then if A is TRUE, there is no need to evaluate B, since the result is always TRUE
My motivation in bringing up parenthesis as not an operator in the above example was to correct the statement:
>> First of All (C||D) will be performed
which is not true, ever.
Thanks for the catch.
to add to above solutions (no points please):
if you have a math background you may exchange the '&&' by * and the '||' by +. then you got:
if there are no paranthesis i remove spaces around the * operator (if using letters for the expressions you even could omit the *).
with that, i can see very easily in which order the expressions were evaluated.
Sara
if you have a math background you may exchange the '&&' by * and the '||' by +. then you got:
A*B + (C + D)
if there are no paranthesis i remove spaces around the * operator (if using letters for the expressions you even could omit the *).
with that, i can see very easily in which order the expressions were evaluated.
Sara
They are logical operators in c
Refer this for more details -
https://www.tutorialspoint.com/cprogramming/c_logical_operators.htm
A && B || (C||D) - do you have any examples on which you are using against these values?
Hope it helps !