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

while loop

 
richard stockman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When trying a simple while loop is it correct that it will not work unless any variables are actually given a value rather then just delaring them as ints.
eg - This will not compile.


Thanks.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a primitive is not initialized, it has no value, therefore it can not be compared, so a compiler error makes sense.

As an aside an easier way to do that without loops is n(n+1)/2.
[ April 23, 2006: Message edited by: Rusty Shackleford ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A local variable has to be initialized by the programmer before you try to use its value.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic