posted 19 years ago
Of course, a do loop always permits the first iteration, so i becomes 2.
"while (b = !b)" is tricky. It contains an assignment expression, not a conditional expression.
If an assignment operator (=) is used in an expression, the value of the right hand side of = becomes the result of the operation.
In this case, "b = !b" takes b, which has the value false, negates it, giving the value true, and stores true in b. However, the value true is also used as the value of the expression "a = !b", so "while(b = !b)" is true and the second iteration of the do loop is permitted. Now i becomes 3.
Now, b is true, so b becomes false and while("b = !b)" is false. So the do loop falls through and i remains 3 and is printed as 3.
[ November 22, 2004: Message edited by: Mike Gershman ]
Mike Gershman
SCJP 1.4, SCWCD in process