# Find th efor loop error (simple!... I assume...)

• 06-22-2007
Schmag
Find th efor loop error (simple!... I assume...)
Noobie query, I'm sure...

Can someone spot the parse errors in the following code. It sems to lie in my one for loop. I wasn't sure how to put multiple statements in a single for loop.

Code:

``` for(i=1; i<=nruns; i++)         {         for(j=1; j<=nsims; j++)                 {                 if(T_mle[i][j] =0.0)                         {                         for(k=1; k<=nsims; k!=j; T_mle[i][k]!=0.0; k++)                                 {                                 T_mle[i][j]+=T_mle[i][k];                                 }                         T_mle[i][j]=T_mle[i][j]/(nsims-p);                         }                  }         }```
So I want the for loop to run for k=1 to nsims, skipping the cases where T_mle[i]k[]=0.0 as well as when k=j.

Thanks guys.
• 06-22-2007
Daved
>> for(k=1; k<=nsims; k!=j; T_mle[i][k]!=0.0; k++)
What are you trying to do there?

>> skipping the cases where T_mle[i]k[]=0.0 as well as when k=j
If you are trying to skip certain cases, you should skip them inside the body of the loop. The stataments in the for loop are only for initialization, termination and increment.

>> if(T_mle[i][j] =0.0)
Don't forget the difference between assignment and equality.
• 06-22-2007
JJFMJR
Hmm the for loop syntax is as followed
for(int x = 0; x <= 100; ++x)
for(Variable ; Repeat While; Increment Variable)

And that's your standard For loop

Yours should be:

Code:

``` for(i=1; i<=nruns; i++)         {         for(j=1; j<=nsims; j++)                 {                 if(T_mle[i][j] == 0.0)                         {                         for(k=1; k<=nsims && k!=j && T_mle[i][k]!=0.0; k++)                                                                                       //If that's what you wanted.                                 {                                 T_mle[i][j]+=T_mle[i][k];                                 }                         T_mle[i][j]=T_mle[i][j]/(nsims-p);                         }                  }         }```
However it is always wiser to keep the for loop statement simple, so it would be better if you skipped or exited the for loop in the body as Daved stated.
I'll let you do that part though.