aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes For Loop 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" Watch "For Loop" New topic
Author

For Loop

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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.
ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
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.


ARS Kumar, Sun Certified Programmer for Java 2 Platform
http://www.automatedsqa.com/
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
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
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
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

Joined: May 22, 2000
Posts: 108
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

Joined: Nov 22, 2008
Posts: 18944
Hi all,
I just execute the code above, it execute once, it is not infinite loop. I am confused the result too.
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
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

Joined: Nov 22, 2008
Posts: 18944
hai ,
i run the program in jdk1.3 win98 and got strange result
infinite loop with 0(zero).
thanks
shiva
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
HAI
i forget to mention the condition that
it ran in to loop woth or without j+=i statement.
thanks
shiva
rexian
Greenhorn

Joined: May 02, 2000
Posts: 25
OF COURSE its an infite loop; with or without j=i+j.
Check OS to figure out if it has restriction on infinite loops:-)

<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR><B>Successful people in this world are those who get up and look for the circumstances they want. If they can't find them, they make them.</B><HR></BLOCKQUOTE>
Anand Iyer
Greenhorn

Joined: Jun 13, 2000
Posts: 26
Hi,
Same with me..executed once with o/p 0 , then terminated..
jdk 1.2 on WinNT

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944

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
Netla Reddy
Greenhorn

Joined: Jun 18, 2000
Posts: 15
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).]
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
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

Joined: Nov 22, 2008
Posts: 18944
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.
pollai mandala
Greenhorn

Joined: Jun 16, 2000
Posts: 17
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..

Regards,<BR>Bhavani
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Ajit Deshpande
Greenhorn

Joined: Jun 15, 2000
Posts: 17
I am using windows 98 and jdk1.2.1
it goes into an infinite loop
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Runs in infinite loop with or without condition j+=i; I tested it on WindowsNT4 Client and jdk1.2
bante
Greenhorn

Joined: Jun 19, 2000
Posts: 11
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.
CSR Rao
Greenhorn

Joined: Mar 31, 2007
Posts: 6
Hai,
I ran the program using visual cafe 3 and got
an infinite loop printing zero .

Regards,<br />CSR.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Runs in an Infinite Loop with or without j+=i.I am running Java 1.2.2 on Windows NT platfrom.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Nov 22, 2008
Posts: 18944
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.
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi all,
this code printed 0 infinte times omn my m/c. i'm using jdk1.3 on winNT 4.0
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
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).]
Babu Ramu
Greenhorn

Joined: Jul 06, 2000
Posts: 1
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
Anil Kollur
Ranch Hand

Joined: Mar 25, 2000
Posts: 101

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


Everyday is a Learning phase! Hence ask "What have I learnt today?"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 25, 2000
Posts: 101

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

Joined: Jun 03, 2000
Posts: 97
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.
michael huang
Ranch Hand

Joined: Jul 30, 2000
Posts: 63
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?
Chamya Pehalvan
Greenhorn

Joined: Jul 10, 2000
Posts: 18
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??

lstrite
Greenhorn

Joined: Jul 10, 2000
Posts: 20
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: For Loop