DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

Thread: Strage problem with while loop (urgent)

1. Registered User
Join Date
Jan 2006
Posts
5

Strange problem with while loop (urgent)

Hi, I have been staring at the screen so long trying to figure out this problem, that I now have a headache. I have included 4 snipets of code which could possible have have something to do with the problem.

Declaration of Variables Snipet:
Code:
```

double[] TRP=new double [100000],
PRP=new double [100000];

int[] E1=new int[100000],
E2=new int[100000],
E3=new int[100000],
I1=new int[100000],
I2=new int[100000],
I3=new int[100000];```
The Initialization Code Snipet (Included within the init() method):
Code:
```    for (int i=0; i<100000; i++){

E1[i]=-1;
E2[i]=-1;
E3[i]=-1;
I1[i]=-1;
I2[i]=-1;
I3[i]=-1;
PRP[i]=70;
TRP[i]=70;
}```
Some code that does stuff (is looped with a timer of zero delay):
Code:
```    int Etemp=-1, Itemp=-1;

for (int i=0; i<TRP.length; i++){
if (i==0){
if (PRP[i]<PRP[i+1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i+1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}

else if (i==99999){
if (PRP[i]<PRP[i-1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i-1]=Etemp; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i-1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i-1]=Itemp; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}

else {
if (PRP[i]<PRP[i-1] && PRP[i]<PRP[i+1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i-1]=Etemp; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i-1] && PRP[i]>PRP[i+1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i-1]=Itemp; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}
}```
The Problem Code:
Code:
```    Etemp=-1;
Itemp=-1;

int j=E1[0],k=I1[0];

boolean Eloop=true,Iloop=true;

while (j>-1){
//some code that does stuff, not an issue because its ignored being stated as a comment

j=E1[j];
if (j==100000)break;
}```
OK, so the problem is that it seems that when ever j becomes 99999 (as far as I can tell), the while loop break conditional is never met and so the while loop proceeds to an infinite loop. I have no idea why, especially since for a while in the begenning, the break statement is met with no problems, its just that as the program keeps looping with the timer, the while loop eventually reaches an infinite loop as j seems to become 99999.

If extra code is required, let me know, and I would of course really appreciate it if someone could help me with this problem. I know its asking a lot, so thanks a bunch in advance
Last edited by hddd12345678910; 01-16-2006 at 08:45 PM.

2. Registered User
Join Date
Dec 2005
Location
New Jersey
Posts
290
Well, looking at your code (unless your setting j to something and you didn't post that) before the loop, j equals -1. So the loop looks like none of it will ever get excecuted. In your loop put:
Code:
`System.out.println("got in loop");`
to see if the loop was ever excecuted. If it's not, right before the loop put:
Code:
`System.out.println(j);`
I think j should be -1. If it's not, post the results.

3. Registered User
Join Date
Jan 2006
Posts
5
You're correct, j is equal to -1 at first, but thats because originally E1 (at all indexes) is set to -1; thus the line j=E1[0] will set j to the value of -1. However, the snipet of code that starts with the line

Code:
`   int Etemp=-1, Itemp=-1;`
changes the values of E1 and I1 at various indexes. Thus as the program proceeds with the help of the looptimer, certain indexes of E1 have values other than -1, which are of course the ones that I wish to consider with the while loop:

Code:
`while (j>-1){`
So yes, I have performed the test using JOptionPane, and the while loop does indead function as it is supposed to for a short period of time, but it seems that when j eventually equals a value of 99999, the while loop begins to loop to infinite for no apparent reason.

4. Registered User
Join Date
Dec 2005
Location
New Jersey
Posts
290
I don't really have time right now to re-evaluate your code, but logically, if j is 99999, and you're setting j to E1[j], and j continues to be 99999, then E1[99999] would equal 99999.

5. Registered User
Join Date
Jan 2006
Posts
5
To understand what's going on with E1, you have to look at the following code:

Code:
```    int Etemp=-1, Itemp=-1;

for (int i=0; i<TRP.length; i++){
if (i==0){
if (PRP[i]<PRP[i+1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i+1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}

else if (i==99999){
if (PRP[i]<PRP[i-1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i-1]=Etemp; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i-1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i-1]=Itemp; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}

else {
if (PRP[i]<PRP[i-1] && PRP[i]<PRP[i+1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i-1]=Etemp; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i-1] && PRP[i]>PRP[i+1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i-1]=Itemp; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}
}```
You may see that as the for loop proceeds to 100000, there is always atleat one index for E1 that has the value of 100000, that's the hypothesis anyway.

More specifically, you can see from the code:
Code:
```      else if (i==99999){
if (PRP[i]<PRP[i-1] && PRP[i]<70){
if (Etemp!=-1){E1[Etemp]=i; E1[i-1]=Etemp; E1[i]=100000; Etemp=i; }
if (Etemp==-1){Etemp=i; E1[0]=Etemp; E1[i]=100000; }
}

if (PRP[i]>PRP[i-1] && PRP[i]>70){
if (Itemp!=-1){I1[Itemp]=i; I1[i-1]=Itemp; I1[i]=100000; Itemp=i; }
if (Itemp==-1){Itemp=i; I1[0]=Itemp; I1[i]=100000; }
}
}```
that if i=99999, then E[i] and similarly E[99999] is always equal to 100000 or -1 depending on whether or not subsequent conditions are met, as far as I can tell, and so when j=E[j] and j=99999, then E[j] should equal 100000, and thus the conditional on the next line:
if (j==100000)
should be satisfied and so the while loop should break. However, rather then breaking, it goes into an infinite loop.

BTW, I'm not setting j to E1[j], but rather the reverse, I am setting E1[j] to j in the following code:
Code:
```    Etemp=-1;
Itemp=-1;

int j=E1[0],k=I1[0];

boolean Eloop=true,Iloop=true;

while (j>-1){
//some code that does stuff, not an issue because its ignored being stated as a comment

j=E1[j];
if (j==100000)break;
}```
I suppose I may afterall be asking too much, thanks for atleast trying though.
Last edited by hddd12345678910; 01-16-2006 at 10:14 PM.

6. Registered User
Join Date
Jan 2006
Posts
5
Actually, never mind, I have been doing lots of various tests and have found that the problem actually lies elsewhere in code that I have not displayed, so I'll figure this out by myself. I appreciate the help though.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise