• 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

Simple do while loop

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the output of the same

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, just by eyeballing it, no compiling or running it, I would say that the answer is three.  What did you expect the answer to be?

Henry
 
Marshal
Posts: 8880
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid I don't understand question, if there is such. But to understand the behaviour of this code snippet, I think the best way is to run the app in debug mode and see what happens.
Otherwise we'd need to run debugger here (in words), but that just would be the debugger's repetition, which you can do yourself.

If unclear, try to tell us line by line what you think happens there.
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yogesh,

Here is the explanation about execution of your program.

Initially b value is false and i value is 1. Next do while loop . do while executes for the first time without condition checking.


Please observe the above do while loop condition. first you are negating b value and  you are assigning that value  to b again.
Iteration 1:
Control enters do while loop. i will becomes 2. next condition checking . while(b=!false) =>while(b=true) =>while(true). condition is true . so loop execution continues.
Iteration 2:
i will becomes 3. next condition checking. Now b value is true because of previous loop iteration. while(b=!true) =>while(b=false) =>while(false). condition is false. so loop execution terminates.
Now i value is 3. So the output is 3. Please correct me if anything is wrong.

Hope this helpful


 
yogesh doshi
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is three@henry

yogesh doshi wrote:What will be the output of the same

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic