**New course!**Every coder should learn

**Generative AI!**

+ 2

# can anyone explain this code?

int a=3,b=4; a+=(-a==1?-3:4); cout<<a; what is the answer and explain it details. thanks 😀

3 Answers

+ 12

Answer is 7
a+=((-a==1)?-3:4);
a=a+((-a==1)?-3:4);
//assigning a value
a=3+((-3==1)?-3:4); // -3 is not equal to 1 so its false
a=3+4;
a=7

+ 8

This code prints out 7.
On the second line: if -a is equal to 1, then the statement in brackets returns -3, if not it returns 4.
Then the return value of the brackets statement is added to the original value of a.
In a nutshell: a is equal to 3, -a is equal to -3, which is different from 1. So the 2nd line gives a = a + 4 = 3 + 4 = 7.

+ 1

thanks a lot! really helpful