| Author |
Doubt on do-while loop
|
meera kanekal
Ranch Hand
Joined: Feb 13, 2005
Posts: 75
|
|
I got this from the enthuware exam on Flow Control
What will be the result of attempting to compile and run the following program?
The correct answer is: It will print 3.
Can any one explain why this does not go into infinite loop?
I tried running this code by making the following change:
and it goes into infinite loop. Is this happening because in the previous version b is reset again in b=!b?
Also the result is because in a do while loop the body of do is executed atleast once, so the first time do-while is executed i=2 and b=true. Therefore the body of do is executed again and i=3 and b=false again. But what is confusing me is what triggers exit of the loop? Can anyone please give me a better explanation?
Thanks,
Meera
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Therefore the body of do is executed again and i=3 and b=false again.
This triggers exit of the loop.
|
SCJP 6
|
 |
alex sandoval
Greenhorn
Joined: Dec 08, 2008
Posts: 26
|
|
Hi Meera, I've created a step-by-step guide of how this program gets executed and how it did prints 3 as the output. Hope this will help. Thanks.
Initialize: i=1
Step 1:
Enter do-while loop then increment i by 1: i=2
Step 2:
While statement will be execute, variable b will be negated so it will be true: b=true
b is true so it will go to do-while body again
Step 3:
Enter do-while loop then increment i by 1: i=3
Step 4:
While statement will be execute, variable b will be negated so it will be false: b=false
b is false so it will now exit do-while loop
Step 5:
Prints: 3
|
 |
sunil langeh
Ranch Hand
Joined: Sep 04, 2007
Posts: 85
|
|
alex sandoval wrote:
While statement will be execute, variable b will be negated so it will be true: b=true
b is true so it will go to do-while body again
Step 3:
Enter do-while loop then increment i by 1: i=3
Step 4:
While statement will be execute, variable b will be negated so it will be false: b=false
b is false so it will now exit do-while loop
Step 5:
Prints: 3
Hi alex, What does it's mean "variable b will be negated so it will be true: b=true" i am not understanding this "negated" please help.
|
Thanks
Sunil (SCJP 5)
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
b is false, then what is the negative means opposite of b? that is true. so !b becomes true.
|
 |
Sachin Adat
Ranch Hand
Joined: Sep 03, 2007
Posts: 213
|
|
The catch here is the assignment operator = and not ==
while (b = !b);
This means the boolean value of expression. So do it while this condition(the value of the boolean expression) is true.
In the 1st iteration b = !b becomes b = !false and b = true.
So the whole expression is true and variable b becomes true.
In the 2nd iteration b = !b becomes b = !true and b = false.
So the whole expression is false and variable b becomes false.
Since the while condition becomes false, it exits the do-while loop.
In the 2 iterations i becomes 3. So it prints 3.
|
SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
|
Please use code tags when you post source code.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Tomasz Sochanski
Ranch Hand
Joined: Jan 13, 2009
Posts: 47
|
|
meera kanekal wrote:I got this from the enthuware exam on Flow Contro
But what is confusing me is what triggers exit of the loop? Can anyone please give me a better explanation?
The result of assigning boolean to variable is the value of assigned boolean - check this:
|
 |
 |
|
|
subject: Doubt on do-while loop
|
|
|