wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop problem" Watch "for loop problem" New topic
Author

for loop problem

Ashok Parakkal
Greenhorn

Joined: Jan 11, 2005
Posts: 7
Why does the following code end up in an infinite loop

for (int i = 1;i>0;i++) {
System.out.println(i);
}
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
always i > 0 because u r doing i++
thats why it goes looping forever
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It's not an infinite loop. Look carefully and think about it. Or if you don't want to wait for it to finish, then start i with the value Integer.MAX_VALUE-42 and try it.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
This might help you understand better, why it stops:
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
yeah u all r right
it finally ends at printing -2147483648
So, after -2147483648 , 0 is the next value, so it ends there as 0 < 0 is false,
good question.

Thanks all
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Animesh Shrivastava:
yeah u all r right
it finally ends at printing -2147483648
So, after -2147483648 , 0 is the next value, so it ends there as 0 < 0 is false,
good question.

Thanks all


Nope,
it stops because -2147483648 > 0 is false. And the next value would not be 0, it is -2147483647.
Ashok Parakkal
Greenhorn

Joined: Jan 11, 2005
Posts: 7
Sorry guys i tried everything..it still is ending up in an infinite loop.
I modified the code ..
for (int i=Integer.MAX_VAL - 5;i>0;i++) {
System.out.println(i);
}

it is still ending up in an infinite loop...
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Yeah Barry,
u absolutely right, i didnt think much abt it and replied,
sorry for that mistake

But a strange thing is happening here.
I am using Eclipse as my editor, i write the same program
int i = Integer.MAX_VALUE - 5;
for (;i>0;i++) {
System.out.println(i);
}
System.out.println(i);

and it runs into an infinte loop, displaying
2147483642
2147483643
2147483644
2147483645
2147483646
2147483647
-2147483648
-2147483647
-2147483646
-2147483645
-2147483644
-2147483643
-2147483642
-2147483641
-2147483640
-2147483639
-2147483638
-2147483637
-2147483636
-2147483635
and so on

But when i run either in a debug mode or i remove the
System.out.println(i);
from inside the for loop block, it displays me the perfect output as
-2147483648

Can anyone explain the reason behind this

Ashok just try writing the program this way:
int i = Integer.MAX_VALUE - 5;
for (;i>0;i++) {
}
System.out.println(i);

U will get the final output as expected
Ashok Parakkal
Greenhorn

Joined: Jan 11, 2005
Posts: 7
Animesh,

I am also using eclipse..i too noticed lot of strange things..for
example the code below doesnt end up in an infinite loop..

int i = Integer.MAX_VALUE-42;
for( ; (boolean)(i > 0); i++ ){
System.out.println( i );
}System.out.println( "i after loop = " + i );
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Works fine for me in Eclipse 3.0.1 and 3.1M4. I cut and pasted your code in the above post.
Ashok Parakkal
Greenhorn

Joined: Jan 11, 2005
Posts: 7
yes barry it works...but if u remove the boolean cast it is not working..at least it is not working in my eclipse..
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There was no boolean cast in the code I pasted.
Ashok Parakkal
Greenhorn

Joined: Jan 11, 2005
Posts: 7
barry,

I think ur right...its an eclipse jvm problem..i tested using jdk 1.3.1 and 1.4 compilers and jvm..its working fine..but the same code behaves differently in my WSAD (version is 5.1.0)..i guess in the latest versions of eclipse they wud have fixed it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop problem