This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Simple do while loop

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output of the same

 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
 
Sheriff
Posts: 7647
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Answer is three@henry

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

 
I've been selected to go to the moon! All thanks to this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic