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

for loop problem

 
Ashok Parakkal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
always i > 0 because u r doing i++
thats why it goes looping forever
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help you understand better, why it stops:
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was no boolean cast in the code I pasted.
 
Ashok Parakkal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic