Given:
int j = 5;
while ( ++j < 9)
cout << j++ << endl;
Why are only 6
and 8
printed?
Upon entering the loop, j
is 5. Then the while (++j ...
increments that to 6. Then the cout
executes, printing out 6. Then the side effect of the j++
takes place, incrementing it to 7.
On the next iteration of the loop, the ++j
increments it again, giving 8
, which is then printed out. Then the side effect of the j++
takes place, incrementing j
to 9.
Then when while (++j < 9)
is executed again, j
has been incremented to 10, so the while
loop exits, and no more is printed.