Suk*_*osh 1 c comparison-operators
int a = 2;
while (1 < a < 5) {
printf("%d\n", a);
a = a + 1;
}
Run Code Online (Sandbox Code Playgroud)
在C程序中,我使用了上面的代码,但它总是无限计数.这意味着while循环的测试条件始终为true.但我无法理解为什么会这样.
1 < a < 5被分组为(1 < a) < 5.
对于你的值a,1 < a返回1(true),所以现在你的表达式变为1 < 5,总是评估为1,这就是为什么你最终得到一个无限循环.
您可以通过编写while((1 < a) && (a < 5))来获得所需的行为.