Consider the following nested if-statements: int x = N; int y = 1; if (++x == 1) y = 2; else if (++x == 3) y = 3; else if (++x == 5) y = 4; else if (++x == 7) y = 5; N is an integer to which x is initialized. Depending on the value of N, y will have some value at the end. Consider the cases where N is 0, 1,..., 10. Then, identify, in the list below, the possible combination of values for N and y. a) N = 2; y = 1. b) N = 5; y = 4. c) N = 2; y = 4. d) N = 4; y = 5.
no alternative
u mean no right answer?
yes, no right answer
Thanks a lot. My professor said every problem has a correct answer, damn I am depressed.
sorry, I was wrong, the correct answer is the letter c) N = 2; y = 4.
Could you explain to me why it is c? I don't understand. Coz I think when N=2 then x+1 = 3 so y = 3.But c) is y=4. How can I understand this?
é meio complicado, numa sequencia de N inicializando em 0 ate 10, trying to show the code: int x = N; N=2 int y = 1; ---- if (++x == 1) y = 2; + + X, increment N +1, then N=3 else if (++x == 3) y = 3; + + X, increment N +1, then N=4 else if (++x == 5) y = 4; + + X, increment N +1, then N=5,,enters the condition, else if (++x == 7) y = 5; ok?
good question!!
Yes c is true.
Join our real-time social learning platform and learn together with your friends!