+ 2

Order of Execution?

What will execute first..? Relational operator or Logical operator.

29th Sep 2020, 1:52 PM
Asrar
Asrar - avatar
12 Réponses
+ 3
The operator precedence can be seen (for example python) in the docs: https://docs.python.org/3.7/reference/expressions.html go to the very bottom of this document. you can find a table there about this issue. keep in mind that the table shows precedence from the lowest to the highest ones.
29th Sep 2020, 2:36 PM
Lothar
Lothar - avatar
+ 3
Netha_r2071 what you think about the output of the statement below, cout<<1&&0; cout<<(1&&0);
29th Sep 2020, 2:30 PM
Asrar
Asrar - avatar
+ 2
Jayakrishna🇮🇳 yes I checked. cout<<1&&0; //Output - 1 cout<<(1&&0); //Output - 0
30th Sep 2020, 4:22 AM
Asrar
Asrar - avatar
+ 2
ASRAR thats a strange output. Not a correct one. I don't why like that happening. No idea. Something other happening.. But the correct way is: Check this, both should get 0. Hoping Some other may answer it.. int main() { int a = 1&&0; cout<<a; //Output - 0 a = (1&&0); cout<<a; //Output - 0 return 0; }
30th Sep 2020, 2:34 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳, ASRAR, in the case of "cout<<1&&0" (output 1) the streaming operator is taking higher precedence over the logical operator. To demonstrate, observe what happens when you use it as a conditional expression: #include <iostream> using namespace std; int main() { if (cout<<1&&0) cout << "true"; else cout << "false"; return 0; } //output: 1false It executes cout<<1, which seems to return a non-null pointer if successful and evaluates as true. Next it evaluates that true with && 0 to get false.
1st Oct 2020, 10:36 AM
Brian
Brian - avatar
+ 1
🎃#H P 22™🎃 (1==1 && 0!=0) If the answer is logical operator, then how the above statement will execute m
29th Sep 2020, 1:59 PM
Asrar
Asrar - avatar
+ 1
Relational operators have higher precedence than logical operators. Logical operators combines the operations which may have relational operators so to evaluate those logical operators, must evaluate first those relational operators.. So relational operator executed first.. Ex : a<b && c>d first a<b then c>d evaluated then &&. Edit : see this for more clarity... https://www.tutorialspoint.com/questions/index.php Edit : ASRAR did you checked this link? For your below code sample, both prints 0.
29th Sep 2020, 2:04 PM
Jayakrishna 🇮🇳
+ 1
29th Sep 2020, 2:06 PM
Asrar
Asrar - avatar
+ 1
FIRST OF ALL ITS NOT EXECUTING, ITS EVALUATING! ASRAR in the given example there are two conditions to be evaluated,two relational and one logical right, so when in parentheses your each of conditions (relational ) evaluates and then it wholly evaluates logical operators, in the above Case relational evaluates first!
29th Sep 2020, 2:07 PM
RuntimeERROR
RuntimeERROR - avatar
+ 1
Jayakrishna🇮🇳 Netha_r2071 🎃#H P 22™🎃 But I tried to execute that statements in different ways. The warning said to use parentheses. It's just executed in the FIFO order.
29th Sep 2020, 2:22 PM
Asrar
Asrar - avatar
+ 1
ASRAR Different ways in the sense?you only have two different statements for a logical operator right? Then what are different ways? and of course u have to use parentheses while writing for a logical statements having more than one conditions,for good readability and good programming practice!
29th Sep 2020, 2:24 PM
RuntimeERROR
RuntimeERROR - avatar