• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While loop - Head's First Java example

 
Gabriel Allen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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?
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Are there any conditionals on these lines?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Great! Thanks! This really helps.
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic