[英]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. 我有以下代码,我在其中运行一个嵌套的for循环,并在循环内检查语句是否为真。 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 ((m = 261) && (k=361))
这是一个分配。
If you want to compare something: 如果您想比较一些东西:
==
==
<
/ >
<
/ >
!=
!=
if(f2 == nullptr)
if(!f2)
Both cases are equal. 两种情况都相等。 If f2 is null, then...
如果f2 为 null,则...
if(f2 != nullptr)
if(f2)
If f2 is not null, then... 如果f2 不为 null,则...
Note: nullptr is the c++11 feature. 注意: nullptr是c ++ 11功能。 If you are not using c++11, use NULL insead.
如果不使用c ++ 11,请使用NULL insead。
Edit: As @unwind commented, there are two assignments. 编辑:正如@unwind评论,有两个作业。
Since 261
is always non-zero, 1st condition is always true. 由于
261
始终为非零,因此第一条件始终为true。 It will continue to evaluate the right hand side and assign 361
to k
. 它将继续评估右侧,并将
361
分配给k
。
your are assigning value to m
here : if( (m = 261) && (k=361))
which is always true (because it is non-zero value). 您在这里为
m
分配值: if( (m = 261) && (k=361))
始终为真(因为它是非零值)。 it should be if( (m == 261) && (k==361))
应该是
if( (m == 261) && (k==361))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.