Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Terminates no endless loop ?

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20993
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies
 
Suzie Russell
Greenhorn
Posts: 22
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Suzie Russell
Greenhorn
Posts: 22
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is that " ; " you put in middle of program mean???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic