Please explain me why in this code output is 14. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please explain me why in this code output is 14.

Such a code: int x = 19; int y = x%3; while (y<3) x -= ++y; Console.WriteLine(x); Visual studio issues 14. I tried to understand the difference between ++y and y++ and could not. Please help me.

23rd Sep 2020, 11:36 AM
Jackson de Frost
Jackson de Frost - avatar
4 Answers
+ 6
You already got detailed explanation about the code from Nasif Rahman 's answer. Further check this lesson.. It'll clear your doubts between prefix and postfix. https://www.sololearn.com/learn/CSharp/2590/?ref=app
23rd Sep 2020, 11:58 AM
Minho
Minho - avatar
+ 3
First y =1 x-=++y //x=19,++y=2(y=y+1),x=x-y=17 x-=++y //x=17,++y(y=y+1)=3,x=x-y=14 I hope you understand!
23rd Sep 2020, 11:49 AM
Vadivelan
+ 2
Thank you very much, guys! Nasif Rahman Vadivelan Can you also describe an exactly the same example, but with y++? Then I will certainly understand everything. Thanks!
23rd Sep 2020, 12:17 PM
Jackson de Frost
Jackson de Frost - avatar
+ 2
https://code.sololearn.com/c8xhfsCXgt74/?ref=app https://code.sololearn.com/cc5B9nQA82yR/?ref=app a = 0 ++a = 1 a == 1 //true b = 0 b++ = 0 (if b++==0 //true, if ++b ==0 //false) b == 1 //true
23rd Sep 2020, 12:28 PM
Vadivelan