Which had higher precedence == or || operator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Which had higher precedence == or || operator?

30th Apr 2020, 4:59 AM
TeaserCode
7 Answers
+ 9
You can easily google the operator precedence table for C++. You can check it here : http://en.cppreference.com/w/cpp/language/operator_precedence
30th Apr 2020, 5:13 AM
Nova
Nova - avatar
+ 1
To find it out if == has lower precedence than a logical OR, run this short snippet: if (0 == 2 || 1) std::cout << "Bjarne\n"; else std::cout << "Stroustrup\n"; If the learbook is right it should print "Stroustrup". And if it's wrong... maybe you should get another book!
30th Apr 2020, 5:24 AM
Damyian G
Damyian G - avatar
+ 1
Damyian G your example is wrong. Regardless of the precedence of == it will always return true. To prove it I forced the precedence below. if (1 || (2 == 0)) std::cout << "Bjarne\n"; else std::cout << "Stroustrup\n"; It will always output Bjarne. This is due to the way || short circuits. Even if the code to the right is ran first it isn't evaluated as far as the logical or is considered. It would then jump back to the left of or and since it has a value of 1, which is equal to true, it jumps out of the condition and never checks the right side of the logical or.
30th Apr 2020, 5:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
if (1 == 0 || 0 == 0) cout << "Bjarne\n"; else cout << "Stroustrup\n"; This might be a better example (I'm sure there are much better ones out there) that shows that == has higher precedence than || edited: had a 1 where 0 was needed. should be correct now.
30th Apr 2020, 6:00 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg - You are right, thanks for the correction. I had the order of my conditions in reverse. Updated my answer.
30th Apr 2020, 6:11 AM
Damyian G
Damyian G - avatar
0
I have a learbook, there is written == has lower precedence than ||, is that true?
30th Apr 2020, 5:16 AM
TeaserCode
0
There shall be grammatical error. I can prove it.
30th Apr 2020, 5:53 AM
TeaserCode