Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817
- Posts: 1
- Joined: Thu Nov 13, 2014 6:48 am
Dears, explain please the next task,
public static void main(String args)
boolean b = false;
int i = 1;
} while (b = !b);
System.out.println( i );
How 'i' gets 3 ?? Yet, (b = !b ) == true, then it should be infinite loop ...
- Site Admin
- Posts: 9170
- Joined: Fri Sep 10, 2010 9:26 pm
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
Users browsing this forum: No registered users and 2 guests