Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

For Loop

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the code below
int i=0,j=0;
while(i+j<20)
{
System.out.println(i+j);
j+=i;
}
This loop executes JUST ONCE(??) when it should be an infinite loop.There is an abrupt completion of the loop.I think the problem is in the j+=i; statement 'cause removing it results in an infinite loop.BUT WHAT IS THE PROBLEM??
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody please help me here.I am giving the exam next week.
I request the moderators of this forum to help me out on this one.
Thank you.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Udayan
I tried your code and its an infinite loop as expected. So I am sorry to say, I didn't get your doubt If you can explain the situation more clearly it will be helpful.
Thanks
ARS Kumar.
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Udayan,
In my case the code behaves in the same wierd way as yours.That is it only prints 0 once and terminates, but if you remove the statement j+=i, the loop executes infinately.This is a strange behaviour indeed and following the logic of the loop I do not yet see why the loop should execute just once.But since ARS Kumar says on his machine it behaves normally, then I dont see whether we need to compare Systems we are using, if it is anything to do that. Let us woo more people to try the code and see if we can have a general behaviour. I will come back when I figure out something.
Regards,
Herbert
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I tried the code on my machine and it goes into an infinite loop,i am using
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)
On Windows NT.
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends
I am using JDK 1.2.2 on a Win98 PC. I am very curious to see the result of this !
Thanks
ARS Kumar
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I just execute the code above, it execute once, it is not infinite loop. I am confused the result too.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ppl
i m getting abnormal behaviour of the program. as it prints O (ZERO) for one time only and then exit the loop
i m using JDK1.2
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ,
i run the program in jdk1.3 win98 and got strange result
infinite loop with 0(zero).
thanks
shiva
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HAI
i forget to mention the condition that
it ran in to loop woth or without j+=i statement.
thanks
shiva
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OF COURSE its an infite loop; with or without j=i+j.
Check OS to figure out if it has restriction on infinite loops:-)
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Same with me..executed once with o/p 0 , then terminated..
jdk 1.2 on WinNT

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I used different jdk compiler to try this code, 1.3 is infinite
and 1.2 is no. I discuss this issue with someone. I think this is
virture machine bug. so it is better to report to sun.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for ur replies.The loop really had me in a tizzy.I will be sure to report this to Sun.I will also quiz some Java experts and get back to all of u with the answer.
Thanks all
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Udayan:
I am on Win '98 running java version given below:
java version "1.2.2"
Classic VM (build JDK-1.2.2-001, native threads, symcjit)
This gives me an infinite loop......
So I don't think I see a prob.
Regds.
- satya
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
I tried with this code.I got same as Udayan was getting ,Like it is executing once and if you remove j+ = i statement it is in infinite loop;
I am using JDK 1.2 and Win NT.
Thanks
Netla
[This message has been edited by Netla Reddy (edited June 18, 2000).]
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Udyan,
With the code you have given,I got 0 printed and when I removed the statement j+=i, it is an infinite loop.
I am using Windows95 and
java version "1.2.1"
Classic VM (build JDK-1.2.1-A, native threads)
Suma
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Sun jdk on Win98
java version "1.2.1"
Classic VM (build JDK-1.2.1-A, native threads)
I still cannot understand it.I have quizzed some java experts on this.Still awaiting the answer.Will get back on this topic asap.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Udyan & All ,
I too tried that on Windows98.It's an infinite loop.
I am using :
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)
its very interesting !
regards..
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I too executed this in JBuilder 2.o ,I got the infinite of '0'.
I am also curious to know the resulit.
Thanks.
/nirmal
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using windows 98 and jdk1.2.1
it goes into an infinite loop
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runs in infinite loop with or without condition j+=i; I tested it on WindowsNT4 Client and jdk1.2
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried my code on my Linuxppc (iMac) JDK1.2, it is normal. I also tried on an Enterprise SUN OS 5 (JDK1.2) and I don't see any problem. 20 is a much larger number compared to 0. If there is a problem, I think SUN deliberately makes trouble for Microsoft.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
I ran the program using visual cafe 3 and got
an infinite loop printing zero .
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runs in an Infinite Loop with or without j+=i.I am running Java 1.2.2 on Windows NT platfrom.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

On an SGI(UNIX), with
java version "1.2"
Classic VM (build JDK-1.2-V, green threads, mipsjit)
I go into an infinite loop, printing 0.
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I tried your code. It gave 0. Jdk version is 1.2.1 on Windows NT4.0. If the j+=i is removed it is going into an infinite loop printing 0 infinite times.
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
this code printed 0 infinte times omn my m/c. i'm using jdk1.3 on winNT 4.0
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It goes into infinite loop with 0 output, with or without j+=i;
I'm running java version "1.2.2"
Classic VM (build JDK-1.2.2-005) on Win NT 4 Client
on PIII 600 MHz.

[This message has been edited by Harry Chawla (edited July 26, 2000).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I executed this in JBuilder 3.0 ,I got the infinite of '0'with the statement or without the statement j+=i;
I am also curious to know the result.
Babu
 
Ranch Hand
Posts: 103
Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! everybody,
I ran the following code in Win98 with jdk1.2.2
public class Forloop {
public static void main(String[] args){
int i=0,j=0;
for(; i+j < 20 {
System.out.println(i+j);
j += i;
}
}
}
and got strange result, only once it executed with 0 output.
Can anybody explain why? it should have been an infinite loop isn't it?
Anil
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,
For all those of you who got a '0' instead of an infinite loop, when you ran it, you are in for another surprise.
As somebody pointed out, the problem does not seem to be in the j+=i; statement. Rather the problem seems to be the type of the variable.
Okay change the original code to look like this.
(I have just changed the type of the variable to float instead of int) and everybody should now start getting infinite loops.
float i=0,j=0;
while(i+j<20)
{
System.out.println(i+j);
j+=i;
}
Intriguing stuff huh ?
Best Regards
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the people who are NOT getting an infinite loop, would you please run it through the debugger and see what values the variables are taking through each iterations.
Thanks!
 
Anil Kollur
Ranch Hand
Posts: 103
Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! all,
I ran the code in two different platforms, LINUX and WIN98,
Linux:
Kaffe Virtual Machine
Engine:Just-in-time ver:1.0b4 java ver: 1.1
and Win98:
Java Version: 1.2.1
Native Threads:1.2.1-A

and got strange results. In Linux its giving infinite loop with output 0 and with Win98 its giving only 1 output with 0.
 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Can some body from the ranch please try to get his hands on this one.
I remeber one such kind of post where a ranch hand was able to figure out that different resultsets on different system were as a result of differnce in speed of PC processor.
Thanks.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, guys
i have execute the code too, it print 0, and infinite loop,
java version "1.2.2"
Classic VM (build JDK-1.2.2_005, native threads, symcjit)
running windows2000 professional
i have looked locals in debugger, there is nothing unusual.
so what about your debugger info?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Udayan,
As per the code....the loop should only run once as there is no condition for the loop to restart(-ie-no increments) and hence after executing once it will exit out if no statements after that.

Originally posted by Udayan:
Consider the code below
int i=0,j=0;
while(i+j<20)
{
System.out.println(i+j);
j+=i;
}
This loop executes JUST ONCE(??) when it should be an infinite loop.There is an abrupt completion of the loop.I think the problem is in the j+=i; statement 'cause removing it results in an infinite loop.BUT WHAT IS THE PROBLEM??


 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chamya,
A loop is not required to have an increment statement.
The following code is valid and results in an infinite loop:
w=5
while (w>0)
{ System.out.println(w);
}
THe issue being discussed here is a problem with certain implementations of java not handling infinite loops correctly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic