Dears, explain please the next task,
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
How 'i' gets 3 ?? Yet, (b = !b ) == true, then it should be infinite loop ...
Thanks
Dmitry
About Question com.enthuware.ets.scjp.v6.2.651 :
Moderator: admin
-
- Posts: 1
- Joined: Thu Nov 13, 2014 6:48 am
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.651 :
In the first iteration i is incremented to 2. Now, b = !b is executed. Here, b is false. So !b is true. Therefore, true is assigned back to b. The value of the expression b = !b is true (because true was assigned to b). So the loop will execute again. i is incremented to 3. Now, b is true. So !b is false. Therefore false is assigned back to b. The value of the expression is therefore false and the loop will terminate.
Thus i will remain 3. There is no infinite loop.
Thus i will remain 3. There is no infinite loop.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 94 guests