What is the order of logical operators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the order of logical operators

AND(&&) , OR(||) and NOT(!)

8th Jul 2023, 1:53 PM
Sooraj Kumar
Sooraj Kumar - avatar
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.
8th Jul 2023, 4:09 PM
A͢J
A͢J - avatar
+ 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
8th Jul 2023, 11:35 PM
A͢J
A͢J - avatar
+ 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
8th Jul 2023, 5:14 PM
Tibor Santa
Tibor Santa - avatar
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
8th Jul 2023, 5:05 PM
Sooraj Kumar
Sooraj Kumar - avatar