About Question com.enthuware.ets.scjp.v6.2.651 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Ianyshev
Posts: 1
Joined: Thu Nov 13, 2014 6:48 am
Contact:

About Question com.enthuware.ets.scjp.v6.2.651 :

Post by Ianyshev »

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.651 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests