+ 5

How does it work?

What is the output of this code? int x = 9; x += ++x; Console.Write(x); //What is the valid output? This was in the C# challenge. I entered 20 and I was wrong!!

20th Apr 2017, 7:34 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
4 Réponses
+ 2
The correct answer is 19. The program first places the value of x in the left hand side and then adds to it the incremented value. It will look like this at the end: 9 += ++9; (= 19)
20th Apr 2017, 7:42 AM
G4S
+ 17
Wouldn't it be 19? 9 + (1+9) => 9+10 = 19
20th Apr 2017, 7:42 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
+ 6
Ok! I was confused because it was pre-increment so I thought x will first increase then assigned. I got it now. Thanks! 😊😉😊
20th Apr 2017, 7:45 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 3
The answer is 19: Let's simplify the statement to: int x = 9; x=x+(++x); which means: x=9+(10)=19
20th Apr 2017, 10:44 AM
Yasser Hosny Abu Elmakarem
Yasser Hosny Abu Elmakarem - avatar