允许在for循环中多个条件? [英] Are multiple conditions allowed in a for loop?

查看:253
本文介绍了允许在for循环中多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code运行没有给出任何错误或警告

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;
}

然而,这本书让我们C(亚什万特Kanetkar)说,只有一个前pression是允许在for循环测试前pression(见本书第115页)。

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).

我不知道的标准。在fo​​r循环测试前pression允许多前pressions?

I am not sure of the standard. Are multiple expressions allowed in the test expression of a for loop ?

编辑:我一定可以连接两个前pressions但我目瞪口呆,当我发现在这上面的code的网站。我的问题是,这是有效的C code或不?

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?

推荐答案

条件

i < p, j < q

是允许的,但可能不是想要的结果,因为它丢弃第一八佰伴pression的结果并返回 J&LT的结果; q 而已。该逗号操作评估的逗号左侧的前pression,丢弃然后计算右侧的前pression并返回。

is allowed but probably isn't what was intended as it discards the result of the first expression and returns the result of j < q only. The comma operator evaluates the expression on the left of the comma, discards it then evaluates the expression on the right and returns it.

如果你想测试多个条件使用逻辑运算符&放大器;&安培; 而不是

If you want to test for multiple conditions use the logical AND operator && instead

i < p && j < q

这篇关于允许在for循环中多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆