C语言 for 循环中是否允许多个条件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17638730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are multiple conditions allowed in a for loop?
提问by Nikunj Banka
The following code runs without giving any errors or warnings
以下代码运行时没有给出任何错误或警告
#include<stdio.h>
int main(){
int i, j;
int p = 0, q = 2;
for(i = 0, j = 0; i < p, j < q; i++, j++){
printf("HelloWorld\n");
}
return 0;
}
However, the book Let Us C(Yashwant Kanetkar)says that only one expression is allowed in the test expression of a for loop.(see pg 115 of the book).
然而,Let Us C(Yashwant Kanetkar) 一书说在 for 循环的测试表达式中只允许一个表达式。(参见本书的第 115 页)。
I am not sure of the standard. Are multiple expressions allowed in the test expression of a for loop ?
我不确定标准。for 循环的测试表达式中是否允许多个表达式?
EDIT : I surely can join the two expressions but I was dumbstruck when I found the above code on this website. My question is that is this valid C code or not?
编辑:我当然可以加入这两个表达式,但是当我在这个网站上找到上面的代码时我傻眼了。我的问题是这个有效的 C 代码是否有效?
回答by simonc
The condition
条件
i < p, j < q
is allowed but probably isn't what was intended as it discards the result of the first expression and returns the result of j < qonly. The comma operatorevaluates the expression on the left of the comma, discards it then evaluates the expression on the right and returns it.
是允许的,但可能不是预期的,因为它丢弃第一个表达式的结果并返回j < qonly的结果。该逗号操作评估的逗号左侧的表达,将其丢弃,然后计算右侧,并返回它的表达。
If you want to test for multiple conditions use the logical AND operator &&instead
如果你想测试多个条件使用逻辑运算符&&,而不是
i < p && j < q
回答by vogomatix
You can link them together with boolean and (&&)
您可以使用布尔值和 (&&) 将它们链接在一起
for(i = 0, j = 0; (i < p) && (j < q); i++, j++){
The above won't print out anything in the loop because the (i < p)condition fails immediately as i & p are both the same value (0).
以上不会在循环中打印出任何内容,因为(i < p)条件立即失败,因为 i & p 都是相同的值 (0)。
Update: Your example is valid (but silly) C because if you start i=30 your loop will still execute 2 times, because the first result in a comma separated list is ignored.
更新:您的示例是有效的(但很愚蠢)C,因为如果您开始 i=30,您的循环仍将执行 2 次,因为逗号分隔列表中的第一个结果被忽略。
回答by Nithin Bhaskar
If you want to test both conditions, use the &&operator.
如果要测试这两个条件,请使用&&运算符。
What is happening in your code is related to how the comma operator ,works.
您的代码中发生的事情与逗号运算符的,工作方式有关。
Both i < pand j < qare evaluated, but only the result of the 2nd expression j < qis checked by the for loop.
既i < p与j < q被评估,但只有第二表达式的结果j < q是由for循环检查。
回答by user2580624
for(i = 0, j = 0; i < p && j < q; i++, j++){
回答by Rohan Mundhey
Even I have read That Book by Mr Yashwant Kanetkar. It do says that only one condition is allowed in a for loop, however you can add multiple conditions in for loop by using logical operators to connect them. In other books that I have Read Along time Ago, said that only one condition is allowed.
甚至我也读过 Yashwant Kanetkar 先生的那本书。它确实说在 for 循环中只允许一个条件,但是您可以通过使用逻辑运算符将它们连接到 for 循环中添加多个条件。在我之前读过的其他书中,说只允许一个条件。

