• 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

While loop - Head's First Java example

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a noob at this, so please bare with me. In the Head's First Java book, there is an example:


Can someone explain to me why the statement "if (x == 2)" doesn't need a "x = x - 1" to get to the final statment?
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Are there any conditionals on these lines?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, walk through the code...

x = 3

while (x > 0) // true!!! x is 3

if (x > 2) // true - x is 3
//we print the a
x = x -1 // x is now 2
// we print the -

if x == 2 // true !!!
//we print the "b c"

if x == 1 //FALSE!!! x is still 2

//we hit the closing brace of the while loop, so loop back up

while x > 0 //true!!! x is 2

if x > 2 //FALSE!!!

x = x -1 // x is now 1

if x == 2 //FALSE

if x == 1 //TRUE

//print d
x = x-1 // x is now 0

//loop back to the top

while x > 0 //FALSE... x is 0, so break out of the loop

//program ends.

Does that make more sense?
 
Gabriel Allen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this makes more sense. Thank you. So, the code will run sequentially throughout the method until the boolean returns "false"?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a while loop:

everything in between those braces (called a "block") will be run over and over, as long as the boolean is true. a common error for beginners is to forget to change the while-condition somewhere in the loop, or have bad logic that never changes it, causing an infinite loop. Note that this loop could run 0 times, if the expression is false the first time we hit it - no line in that block will execute even once.

something you can try is to pepper the code with a bunch of println statements of your own. something like this:
note the "spot 1" and "spot 2". there are two places where x can change, and you might want to know when one fires vs. the other.

then you can re-compile and run the code, and see exactly what happening.
[ January 09, 2007: Message edited by: Fred Rosenberger ]
 
Gabriel Allen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks! This really helps.
 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it depends on the condition/logic of the program to include the x=x-1 into the code
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gurudhaasan,

Please read our Naming Policy. There, you will find the link that will let you change your screen name to comply.

Thanks
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic