Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop

 
nathu nathu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

Cheers,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ) ??

Thanks,

Pete
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

01111111111111111111111111111111

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

10000000000000000000000000000000

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

11111111111111111111111111111111

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

00000000000000000000000000000000

which is of course zero!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic