class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
Since in this question the while(b=!b) is determining the loop, shouldn't the correct answer be infinite loop? There is no clause that terminates the loop?
No, you need to look at the value of the expression (b = !b) as a whole. To begin with, b is true. So !b is false. Therefore, false is being assigned back to b. Therefore, the value of the expression b = !b as a whole is false. So the loop will not be executed again.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Actually to begin with the value of boolean variable b is false...
I ran this in Eclipse's Debug feature and found that it changes to true then back to false and stops when it returns to false. So it goes.. 1) runs initial do with b = false 2) runs again with b = true 3) runs again but b = false so it stops.
This is a crazy boolean expression. I think I get it now.
We need to count how many times do-while loop runs. First check: b is assigned to true (b = !initial value of b(=false)), that's why loop goes again, but second check stops the loop: b is assign to false (b = !previous value of b(=true)).
Loop's body goes two times, that's why correct answer is 1+2 = 3.
This one stumped me for a time and then it finally occurred to me how it worked; at least to my own understanding.
The key to the entire question is hinged on the assignment operator (=) in the while loop.
Variable b's value is set to false entering the loop and is then assigned a value of 'true' when it enters the while loop. i's value is incremented to 2 and the it enters the loop another time.
i's value is incremented to 3.
This time b's value is assigned false when the expression is executed and the while condition evaluates to false and the loop exits.
Yes, that is correct. Only thing is the way you've written " b's value is set to false" and "b's value is assigned false" is not proper. You should write, "b is set to false" or "b is assigned false".
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
class P3{
public static void main(String args[]){
boolean b = false;
System.out.println("before do loop "+b);
int i = 1;
do{
i++ ;
System.out.println("inside loop "+b);
System.out.println("i "+i);
} while (b = !b );System.out.println("ouside loop "+b);
System.out.println( i );
}
}
Made changes to understand,
Output:
before do loop false
inside loop false
i 2
inside loop true
i 3
ouside loop false
3
Don't understand why inside do loop it is true and outside it is false.
Does while has anything to do with it?
class TestClass{
public static void main(String args[]){
boolean b = false;
int i = 1;
do{
i++ ;
} while (false = !false); // This is the line of code that breaks my head
System.out.println( i );
}
}
simpler way to see this question is to read what Oracle says about while loops:
"The while and do-while Statements
The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
while (expression) {
statement(s)
}
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block."
Due to previous a do while loop woks as long as condition is evaluated to true and when it's false will stop.
In our case the boolean value of b is first set to false. Afterward the program it's entering the loop and increments the integer value. At the end of first loop b has true value due to b=!b. At the second loop the value of i is incremented to 2 and at the end of first loop b is false because of b=!b. Finally it exists due to false value of do-while condition.