*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Terminates no endless loop ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Terminates no endless loop ?" Watch "Terminates no endless loop ?" New topic
Author

Terminates no endless loop ?

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

class Warning
{
public static void main(String args[])
{
for(int x=1;x>0;x++)
{;}
}

}

I was thinking this will result in endless loop .
This terminates after some time does it because when after some time x reaches int`s maximum value it terminates


Oracle Java Programmer , Oracle SQL Expert
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

Mahtab Alam wrote:
I was thinking this will result in endless loop .
This terminates after some time does it because when after some time x reaches int`s maximum value it terminates


int variables roll over -- adding one to MAXINT will get you MININT.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

Thanks for your replies
Suzie Russell
Greenhorn

Joined: Mar 13, 2013
Posts: 22

Henry Wong wrote:
Mahtab Alam wrote:
I was thinking this will result in endless loop .
This terminates after some time does it because when after some time x reaches int`s maximum value it terminates


int variables roll over -- adding one to MAXINT will get you MININT.

Henry

If I use something like "System.out.println(x);" in the loop, it is an endless loop but not the otherwise, why?


Sincerely,
A Whovian on a JVM.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

If I use something like "System.out.println(x);" in the loop, it is an endless loop but not the otherwise, why?


As Henry already explained, it's not an endless loop. But slowing down the execution with a sysout may just mean it takes hours, days or even weeks to terminate.


luck, db
There are no new questions, but there may be new answers.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Suzie Russell wrote:If I use something like "System.out.println(x);" in the loop, it is an endless loop but not the otherwise, why?

How do you know it is endless? Have you waited until the end of time, or did you just get tired of waiting and kill it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Suzie Russell
Greenhorn

Joined: Mar 13, 2013
Posts: 22

fred rosenberger wrote:How do you know it is endless? Have you waited until the end of time, or did you just get tired of waiting and kill it?

Don't really guess I have..
Ashish Anant Patil
Greenhorn

Joined: Mar 17, 2013
Posts: 7
what is that " ; " you put in middle of program mean???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Terminates no endless loop ?
 
Similar Threads
PRoblem On threads
while loop
Kathy Sierra doubt
Beginner trying to learn looping but having trouble understanding
endless loop question