+ 3

X++ or ++X

Can anyone explain why this code returns 2 ? https://code.sololearn.com/cc4Fy902dPCN/?ref=app

29th Oct 2017, 3:46 PM
Bruno Carmo
Bruno Carmo - avatar
3 Answers
+ 3
b=++a here ++a is pre increment operator so it increased the value of a by 1 and assigned to b. so the value of a and b is now 1 then a+b becomes 2
29th Oct 2017, 3:49 PM
Nanda Balakrishnan
+ 3
I don't know C#, but I think here is the same case like in Java so I can tell you. When you write b = ++a, what you basically do is: b = a + 1 and also you do a = a + 1. So then the value of both, a and b, is a+1 = 0+1 = 1. So then a+b = 1+1 = 2.
29th Oct 2017, 3:54 PM
Tim Thuma
Tim Thuma - avatar
0
Hi
30th Oct 2017, 3:22 PM
Josias Macosso