简体   繁体   中英

If statement in for loop -infinite loop

I have the following code where i run a nested for loop and inside the loop i check if the statment is true. If so, a kernel is loaded and convolution is performed with the kernel. Primary is the input image.

When i run the code, it becomes an infinite loop. I do not understand why.

for(m=0;m<h;m++)
{
    for(k=0;k<v;k++)
    {        
        if( (m = 261) && (k=361))
        { 
            printf("%f", Thickness[m][k]);
            printf("%d,%d",m,k);
            sprintf(fn,"F:\\newresult\\fft\\neww8_25.raw");
            if ((f2=fopen( fn, "rb" )) == NULL)
            {
                printf("Pb lecture fichier %s\n", fn);
                exit(1);
            }
            for(int a = 0; a < krow; ++a)
                for(int b = 0; b < kcol; ++b)
                    fread(&Kernel[a][b], sizeof(float), 1, f2);
            fclose(f2);
            for(int a = 0; a < krow; ++a)
                for(int b = 0; b < kcol; ++b)
                    sum+=Kernel[a][b];
            for(int a = 0; a < krow; ++a)
                for(int b = 0; b < kcol; ++b)
                    Kernel[a][b] /= sum;
            //convolute 
            s1 = convolve2D(primary, s1, m, k, Kernel, krow , kcol);
        }
    }
}

if ((m = 261) && (k=361)) This is an assignment.
If you want to compare something:

  • for equality, use ==
  • for less/greater: < / >
  • for non-equality: !=

if(f2 == nullptr)
if(!f2)

Both cases are equal. If f2 is null, then...

if(f2 != nullptr)
if(f2)

If f2 is not null, then...


Note: nullptr is the c++11 feature. If you are not using c++11, use NULL insead.

Edit: As @unwind commented, there are two assignments.
Since 261 is always non-zero, 1st condition is always true. It will continue to evaluate the right hand side and assign 361 to k .

your are assigning value to m here : if( (m = 261) && (k=361)) which is always true (because it is non-zero value). it should be if( (m == 261) && (k==361))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM