This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Doubt on do-while loop

meera kanekal
Ranch Hand
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
Posts: 952
Therefore the body of do is executed again and i=3 and b=false again.

This triggers exit of the loop.

alex sandoval
Greenhorn
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
Posts: 88
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.

Punit Singh
Ranch Hand
Posts: 952
b is false, then what is the negative means opposite of b? that is true. so !b becomes true.

Ranch Hand
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.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Please use code tags when you post source code.

Tomasz Sochanski
Ranch Hand
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: