aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Puzzling question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Puzzling question" Watch "Puzzling question" New topic
Author

Puzzling question

Jesse Torres
Ranch Hand

Joined: Mar 25, 2004
Posts: 985
The following code enters the while loop and immediately increments age. So age�s first print is 13.


However, if I put the increment (age++) below the else statement, the while loop becomes an infinite loop, endlessly printing �Obtain your Drivers License."




Why does it become an infinite loop?

Thanks,
Jesse Torres
Ranch Hand

Joined: Mar 25, 2004
Posts: 985
I noticed that if I comment out "continue outer" after the first if statement, the while loop behaves fine.



I still don't understand, why the loop with "continue outer" and the increment age++ on the bottom, goes into an infinite loop.

Thanks,
[ May 25, 2005: Message edited by: Jesse Torres ]
Roman Tomach
Greenhorn

Joined: Mar 17, 2005
Posts: 15
Because after "continue outer" the value of "age" is still 16. So it never be incremented.
Jesse Torres
Ranch Hand

Joined: Mar 25, 2004
Posts: 985
Originally posted by Roman Tom:
Because after "continue outer" the value of "age" is still 16. So it never be incremented.


That makes sense. Thanks. However, why aren't the values that are less than 16 being printed before age reaches 16 for infinite example?

Thanks,
[ May 25, 2005: Message edited by: Jesse Torres ]
Ashok Kumar
Ranch Hand

Joined: Aug 27, 2004
Posts: 93
The values less than 16 do print on the screen.Since it enters a infinite loop you might have missed the initial part of it.IF you are viewing the o/p on a command prompt then increse the buffer size and terminate the program and scroll up.


"Decide what you want, decide what you are willing to exchange for it. Establish your priorities and go to work."
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Now you know why the JavaRanch style guide forbids the use of continue in the middle of a loop. Can make for some difficult code to understand.
Jesse Torres
Ranch Hand

Joined: Mar 25, 2004
Posts: 985
Thank you all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Puzzling question