• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Infinite for loop

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i tried to run the following piece of code in my program, but it turns out to be a infine loop, can anybody throw some light why this is happening.

for ( long i=Long.MAX_VALUE -2; i<=Long.MAX_VALUE; i++ )
{
/* ...*/
}

There are similar problems with Integer.MAX_VALUE, Long.MIN_VALUE and Integer.MIN_VALUE.

Regards,
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hint: what is the value of Long.MAX_VALUE plus 1?
[ June 18, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Gaunt's hint will certainly answer your question. It is also important you to understand the order in which expressions in the for are evaluated.

  • First your initialization
  • Then check the condition expression
  • After that the for block is executed
  • The increment expression is evaluated
  • And then the condition expression again (if met it enters the loop again)


  • [ June 18, 2005: Message edited by: Edwin Dalorzo ]
     
    Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic