aspose file tools*
The moose likes Beginning Java and the fly likes While loop - Head's First Java example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While loop - Head Watch "While loop - Head New topic
Author

While loop - Head's First Java example

Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
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

Joined: Aug 31, 2006
Posts: 226


Are there any conditionals on these lines?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

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

Joined: Jan 09, 2007
Posts: 12
Great! Thanks! This really helps.
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

it depends on the condition/logic of the program to include the x=x-1 into the code


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Gurudhaasan,

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

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: While loop - Head's First Java example