+ 1

If

Who can help me? int a=7,b=1; if(a=8 || b==5) cout<<a*b; cout<<a;

20th Mar 2019, 5:26 PM
Максим Антонюк
Максим Антонюк - avatar
8 Answers
+ 6
Operator precedence. || (logical or) has a higher precedence than = (assignment). So the expression if(a=8 || b==5) is the same as if(a = (8 || b==5)). Note that you're using the assignment operator = instead of the comparison operator ==. The expression (8 || b==5) evalutes to true, or 1 as an integer. So what if(a=8 || b==5) does is set a to 1 and execute the next line, because the whole expression evalutes to true. So the output is (1*1=)1. The last line prints (a=)1 again => output: 11.
20th Mar 2019, 6:12 PM
Anna
Anna - avatar
+ 3
Very very thank you
20th Mar 2019, 6:15 PM
Максим Антонюк
Максим Антонюк - avatar
+ 2
I am seeing no question.
20th Mar 2019, 5:48 PM
HonFu
HonFu - avatar
+ 2
Run this in Code Playground or compile it on your computer, then you'll see.
20th Mar 2019, 5:51 PM
HonFu
HonFu - avatar
+ 2
There it turned out 1 1
20th Mar 2019, 5:52 PM
Максим Антонюк
Максим Антонюк - avatar
+ 2
But I do not understand how?😒
20th Mar 2019, 5:53 PM
Максим Антонюк
Максим Антонюк - avatar
+ 1
cout<<a*b; cout<<a;
20th Mar 2019, 5:49 PM
Максим Антонюк
Максим Антонюк - avatar
+ 1
what is the output of this code?
20th Mar 2019, 5:50 PM
Максим Антонюк
Максим Антонюк - avatar