0

int a = 4; int b = 6; b = a++; Console.WriteLine(++b); == 6?

int a = 4; int b = 6; b = a++; Console.WriteLine(++b); shouldn't the output of this be 6? b = a++, since a is 4 and you add 1 to it it's 5 and "Console.WriteLine(++b)" adds one more? Am I really that retarded? >_> pls some1 enlighten me about why this is 5

17th Nov 2016, 5:01 PM
Róbert Lowell Szenczi
Róbert Lowell Szenczi - avatar
6 Réponses
+ 13
When you see a++ it means we use the value of a and then a increments.. So b = a++ means that we assign a to b and only after that the value of a increments.. so after the line b = a++, b becomes 4 and a becomes 5.. ++b means we should add 1 to the value of b before using it's value.. so b becomes 5 and the output is 5.
17th Nov 2016, 5:48 PM
Rebeka Asryan
Rebeka Asryan - avatar
+ 6
A is 4. B is 6. You set B's value to A's value and then incremented A. A is now 5. B is now 4. You increment B and then print it to the screen. A is 5. B is 5.
17th Nov 2016, 5:08 PM
Ben
Ben - avatar
+ 1
a++ first assigns the value and then increments the value of a whereas ++a first increments the value of a and then assigns it.
17th Nov 2016, 5:13 PM
Ishpreet Singh Bindra
Ishpreet Singh Bindra - avatar
+ 1
i think, in this question, we should know, the difference between varibale++, and ++variable. b=a++, the program b is now 4, after that a is 5, ++b ==> b=b+1; we know, b was 4, we put the value, b = 4+1; so b = 5;
17th Nov 2016, 7:02 PM
Ozan Karataş
Ozan Karataş - avatar
0
İnt a = 4 İnt b = 5 a=b what is a variable
22nd Nov 2021, 5:10 PM
Bersu Bekar
0
İnt a = 4; İnt b = 5; b=a; What is a variable
22nd Nov 2021, 5:11 PM
Bersu Bekar