+ 1

This is a SoloLearn Question, is my answer wrong?

what is the out put of this code? int n = 5; int m = 7; int z = 6; if (++n < m & z==n++) Console.Write(m); else Console.Write(n); My Answer: 7

28th Apr 2017, 9:30 PM
eric mensah
eric mensah - avatar
10 odpowiedzi
+ 11
yeah thank you @jeth
29th Apr 2017, 1:04 AM
Min Somai
Min Somai - avatar
+ 9
++n and n++ do you know the difference . you are wrong there in prefix and suffix.
29th Apr 2017, 12:03 AM
Min Somai
Min Somai - avatar
+ 9
There is a big difference between postfix and prefix versions of ++ . In the prefix version (i.e., ++i ), the value of i is incremented, and the value of the expression is the new value of i . <---. This one, the value is new. In the postfix version (i.e., i++ ), the value of i is incremented, but the value of the expression is the original value of i . Let's analyze the following code line by line: int i = 10 ; // (1) int j = ++i; // (2) int k = i++; // (3) 1. i is set to 10 (easy). 2. Two things on this line: i is incremented to 11 . The new value of i is copied into j. So j now equals 11 . 3. Two things on this line as well: i is incremented to 12 . The original value of i (which is 11 ) is copied into k . So k now equals 11 . So after running the code, i will be 12 but both j and k will be 11. The same stuff holds for postfix and prefix versions of -- . @copyright of stackoverflow
29th Apr 2017, 12:24 AM
Min Somai
Min Somai - avatar
+ 1
Your answer is right.
28th Apr 2017, 10:08 PM
Jeth
Jeth - avatar
0
but, solo says I'm wrong.
28th Apr 2017, 10:11 PM
eric mensah
eric mensah - avatar
0
Solo says incorrect. You can test this code in IDE and see that it outputs 7.
28th Apr 2017, 10:12 PM
Jeth
Jeth - avatar
0
yeah I did that.
28th Apr 2017, 10:13 PM
eric mensah
eric mensah - avatar
0
++n increments value BEFORE comparing while n++ do it AFTER comparing.
29th Apr 2017, 12:06 AM
Jeth
Jeth - avatar
0
So did you read that and understood why answer is 7? Here is playground test btw: https://code.sololearn.com/ceGjD6IRqhLs/?ref=app
29th Apr 2017, 12:29 AM
Jeth
Jeth - avatar
0
yeah thanks @jeth
29th Apr 2017, 6:35 AM
eric mensah
eric mensah - avatar