Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,683 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,232 people online right now. Registration is fast and FREE... Join Now!




endless loop

 
Reply to this topicStart new topic

endless loop

amitdhar
post 5 Sep, 2008 - 01:33 AM
Post #1


New D.I.C Head

*
Joined: 1 Feb, 2008
Posts: 32



Thanked 1 times
My Contributions


CODE

for(int i=1; i>0; i++)
{
}


According to me the above loop is NOT an example of endless loop, because as i is declared int when it crosses the maximum range of int it will give error and will stop.
But while executing it works as an endless loop, after it reaches the int limit, the i value once again starts automaticaly from 1, once again.
Could you plz explain whats wrong in my logic?
User is offlineProfile CardPM

Go to the top of the page

RedSonja
post 5 Sep, 2008 - 04:58 AM
Post #2


D.I.C Head

Group Icon
Joined: 4 Sep, 2008
Posts: 170



Thanked 3 times

Dream Kudos: 25
My Contributions


That is amusing. I would have expected an overflow exception of some kind. Anyway, it overflowed, like the clock when it crosses midnight, lost the big part, and started at 0 again. I guess you were just lucky you did not overflow into something important.

To be really infinite, just do

while ()

I think that would do the trick. In fact I have one of those running somewhere. Wonder if it's still going? No, it does a break when an interrupt comes.
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 5 Sep, 2008 - 05:30 AM
Post #3


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


When it flips, it goes negative.

e.g.
java

int last=-1;
int i;
for(i=1; i>0; i++) {
last = i;
}
System.out.println("last = " + last);
System.out.println("i = " + i);


Results:
CODE

last = 2147483647
i = -2147483648


This post has been edited by baavgai: 5 Sep, 2008 - 05:32 AM
User is offlineProfile CardPM

Go to the top of the page

1lacca
post 5 Sep, 2008 - 05:32 AM
Post #4


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,822



Thanked 11 times
My Contributions


while(){} is not valid in Java, it needs an expression, so it should look-like this: while(true){} Also, in Java integer types can overflow, they won't overwrite anything, it's in the definition of the language. Java is not as brave as C/C++.

amithdar: Are you sure you are running your program correctly?
Dam, baavgai beat me to it, and his solution is nicer.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:54AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month