Didn't understand something | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Didn't understand something

What is the output of this code? int a = 4; int b = 6; b = a++; Console.WriteLine(++b); The answer is 5, But I don't understand why it's that

14th Apr 2017, 1:26 PM
Daniel Kurbatov
Daniel Kurbatov - avatar
4 Answers
+ 4
Calvin's answer doesn't really explain why. The increment operator (++) increases the value by one, but depending which side it's on changes whether the increment happens before or after the value is acted on. (evaluated, assigned, etc.) This is what A++ actually does: int B = A; A = A + 1; This is what ++A does. A = A + 1; int B = A; Or in a single line: int B = (A = A + 1);
14th Apr 2017, 5:05 PM
Neen
Neen - avatar
+ 2
b is 4 after b=a++; b is 5 after ++b
14th Apr 2017, 1:32 PM
Calviղ
Calviղ - avatar
0
Thanks really helped me out
14th Apr 2017, 1:35 PM
Daniel Kurbatov
Daniel Kurbatov - avatar
0
It's still hard to understand at sometimes :D.
22nd Jan 2018, 5:07 PM
Stresas