posted 22 years ago
<pre>
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
</pre>
The catch here is "while (b = !b)" note the equal to sign. its not "==" but is a single "=".
Since its a do-while loop, the loop is executed atleast once..
so after the first iteration the value is 2.
Now whether the loop will get executed again depends on the result of "while (b = !b)"
The return value of "b = !b" is true, i.e the value on the 'Right hand side', which is !false , = true.
so the loop will be executed one more time, during which i is incremented again and the value of i at the end of loop is 3.
Now the condition "b = !b" is checked again.
The resulting value is false, coz b is true, !b is false,
and the value of "b = !b" is equal to the value on the 'Right hand side' of '=' , therefore false and therfore thats the end of loop.
Finally we have i value equal to 3.
HTH,
Rex
[This message has been edited by Rex Rock (edited July 11, 2001).]
[This message has been edited by Rex Rock (edited July 11, 2001).]