Hi, All Please help me to understand this program.
class BoleanTest {
public static void main(String args[]) { boolean b = false;
int i = 1; do{ i++ ; } while (b =!b); System.out.println( i );
} }
O/P is 3 Why???
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
I think you'll learn more if you first describe to us what you think the output should be, and why. Then we can help you understand what the program does.
public static void main(String args[]) { boolean b = false;
int i = 1; do{ i++ ; } while (b =!b); System.out.println( i );
} } You know that do while loop executes atleast once: so for the first time i is incremented to 2. Now the while condition. b =!b.... This is executed as b = (!false) = true. So again loop is executed and i is incremented to 3. Next the condition becomes as b = (!true) = false. Control comes out of the loop. Finally i is printed which is 3.