File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes while loop 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 " Watch "while loop " New topic

while loop

nathu nathu

Joined: May 23, 2005
Posts: 1
void Looping ( )
short t = 1 ;

while ( ++y > 0 ) ;
System . out . println ( " might print" ) ;

The above code prints " might print" and quits but if I use long/int instead of short( for variable t)the loop goes infinite.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263

The problem is the line containing the while. Because you have a semicolon at the end of the line, that statement is the complete while loop. It is essentially a no-op delay loop. When using a short, it comletes after 256 iterations and the print statement is executed.

When used with an int or long, it appears to hang because the while will take a while to complete before the print statement is executed.

Remember, Java executes the next statement after the while. In your case, you've coded a null statement by placing the semicolon right after the while. Java looks at the semicolons - the indenting is just to make it easier for us to read it. Java ignores whitespace.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
marc weber

Joined: Aug 31, 2004
Posts: 11343

As Tom pointed out, your while loop (as shown) doesn't do anything because it's followed immediately by a semicolon.

If you remove the semicolon (and assuming that you want to increment t rather than y), it will execute an iteration for each value greater than 1 within the variable type range -- at which point the value "overflows" to a negative and fails the test. For wider types, this might appear infinite at first, but the loop will end if you give it time.

Try changing the type of t in the following code...

[ May 23, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
How is this loop going to end? I would think that as long as you keep ++t it will always be larger than 0... ( ++t > 0 ) ??


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Large positive numbers "wrap around" to large negative ones. If you add one to Integer.MAX_VALUE (the largest positive int value) you get Integer.MIN_VALUE (the largest negative int value.) The binary numeral for the largest int is a zero followed by 32 ones:


add one to this, and the value overflows, and you get


which is the largest negative int. If you keep adding one, eventually you get


which is -1. Add one to this, and you get


which is of course zero!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: while loop
It's not a secret anymore!