+ 2

Why the value of x print 10 first?

int x=10; Console.WriteLine(x++); Console.WriteLine(x); OUTPUT 10 11

11th Feb 2018, 6:24 PM
UZair AZmi
UZair AZmi - avatar
3 odpowiedzi
+ 1
because x++ perform the operation first means it print the existing value is x and then increase the value is x by 1 and assign it to x. That's why it print 11 next time...
11th Feb 2018, 6:27 PM
Faisal Rahman
Faisal Rahman - avatar
+ 1
x++ post increment which will assign the value to variable and increment it x=x+1 above method execute as x=x which is x=10 then x+1 increments the value like 10+1, 11 so you getting the way output however pre increment was used to increment the value then assign it to variable
12th Feb 2018, 6:53 AM
Jameel udeen
Jameel udeen - avatar
0
x++ is a post increment, meaning it increments after x is called. If you want to increment the number first use ++x, which is a pre- increment.
11th Feb 2018, 6:40 PM
Jose Ayala
Jose Ayala - avatar