+ 1
Why the second output is different??🤔
https://code.sololearn.com/cEOUjwBEkfSA/?ref=app So, first line we get "Hi 1" as output Then, why in second line it shows "Hi 5"?? Shouldn't it show "Hi 5 1" or "Hi 1"?? Edit- I have edited my code
5 ответов
+ 4
In your question values printing in the bases of priority
int x=5;
cout<<"Hi "<<(x && true)<<endl;
Here x is decleared as 5 which is positive integer so every positive number with condition treat as true either 1 so first here in first cout statement u y used brackets so brac will solve first and result will be Hi 1 .
But
cout<<"Hi "<<x && true;
Here u removed brac and << this extraction operator has highest priority so its printing 5 and 1
And work as multiply 5*1 and or as a plus so result is 5
+ 2
A.S. I agree with your analysis up until this nonsense statement:
"... so its printing 5 and 1
And work as multiply 5*1 and or as a plus so result is 5"
Let's look again at the line
cout<<"Hi "<<x && true;
This is evaluated as a Boolean expression with two operands,
<A> && <B>;
First it evaluates <A>, which is
cout<<"Hi "<<x.
The output is "Hi 5", and <A> evaluates to a non-null pointer. For Boolean purposes the pointer reduces to true.
Then it evaluates <B>, which is simply
true.
Finally, <A>&&<B>, i.e., true&&true, reduces to true.
There is no further action to take with the expression so execution proceeds to the next statement without using the final true result.
0
A.S. 🤔 It's complicated but thanks 🙂
0
A.S. As you said, every positive number treated as true .. then why y && true is treated as true??
And false is equeals to 0..
Then x && false should be 0 or y && false should be 0 but why it shows 1??
0
Samael Morningstar read about logical operators you will understood better .
5&&true will give true here 5 is positive use bracket when u print values
5&&0 will give 0 or false
5||0 will give true ==1