+ 1

[c++] please explain me why the answer is 2

int c,j=0,n=0; for(c=1;c<=5;c++){ j=j++; } n=j+2; j=n; cout<<j;

3rd May 2017, 3:15 PM
Ibizaj
Ibizaj - avatar
3 Answers
+ 2
Because, loop does not increase j value. j++ means it will increase value of j by 1 on next cycle. But j=j++, does not increase its value. After that n=j+2; it make n = 2. At last j=n. So, j =2 as output
3rd May 2017, 3:21 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 4
@Aditya this should work according to you but it is not working https://code.sololearn.com/ciBxIDas5kx9/?ref=app
3rd May 2017, 6:26 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
0
it will be 6
24th Sep 2017, 7:43 PM
Ali
Ali - avatar