New course! Every coder should learn Generative AI!
Try a free lesson+ 1
What is the order of logical operators
AND(&&) , OR(||) and NOT(!)
5 Answers
+ 4
How you will define order if condition is like this
if(a && b) {
} elae if (a || b) {
} else if (!false) {
}
Or
if(a || b) {
} elae if (a && b) {
} else if (!false) {
}
Different condition different order
There is no fixed order but boolean operators always execute from left to right. so if && comes first then &&, will execute
if || comes first then || will execute.
+ 2
Sooraj Kumar
As I said left to right so if X is true then
True will be return as per your given example and rest will not execute means Y && !Z are nothing if X is true
decision always happens from left
+ 1
This is decided by "operator precedence" rules. It could be slightly different in every language.
Here is the documentation for C:
https://en.cppreference.com/w/c/language/operator_precedence
! (not) comes first
&& comes next
|| comes last
(do not confuse || vertical bars with lowercase L characters!)
In your example:
X=true, Y=false and Z=true
X || Y && !Z = X || (Y && (!Z)) = true
0
If you say the operator which
comes first will execute or from left to right.
Then how you will decide this problem
X=true, Y=false and Z=true
X ll Y && !Z