int a=-1; cout<<(a++&&1); why output is 1?not 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int a=-1; cout<<(a++&&1); why output is 1?not 0?

tanks

12th Sep 2017, 11:13 AM
pro
pro - avatar
4 Answers
0
why did you add &&1 for ? and a++ mean you would print a then add by 1. Thats why it's not 0, meaning if u print a again it will be 1. Use ++a instead which will add by 1 then print a.
12th Sep 2017, 1:19 PM
jayrn02
jayrn02 - avatar
0
You are using a logical operator, which will return a boolean value (true or false). In C++, 0 is equal to false and every other number equal to true. The "and" operator returns true of both values are true, otherwise false. So -1 is true AND 1 is true, so the expression is true. As a result, the value 1 is printed to the screen. Hope this helps.
12th Sep 2017, 2:11 PM
Shadow
Shadow - avatar
0
@Naitoma,jayrn02 what is the function(why we need it) of && at this moment?from sololearn l know ,only that &&is using when we are working with conditions-if(5>3&&4<6){cout<< ....
12th Sep 2017, 4:53 PM
pro
pro - avatar
0
yes && is for conditions which means and. You usually use it if you want 2 conditions to be true and if 1 is false and the other is true, its consider false regardless which is true
13th Sep 2017, 5:24 AM
jayrn02
jayrn02 - avatar