+ 1
If
Who can help me? int a=7,b=1; if(a=8 || b==5) cout<<a*b; cout<<a;
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.
+ 3
Very very thank you
+ 2
I am seeing no question.
+ 2
Run this in Code Playground or compile it on your computer, then you'll see.
+ 2
There it turned out 1 1
+ 2
But I do not understand how?😒
+ 1
cout<<a*b;
cout<<a;
+ 1
what is the output of this code?