• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on do-while loop

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Therefore the body of do is executed again and i=3 and b=false again.



This triggers exit of the loop.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b is false, then what is the negative means opposite of b? that is true. so !b becomes true.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic