c while循环入口

标签 c while-loop

有人请解释一下为什么

int i=0,j=10;
    while(j>0,i++){
    printf("%d%d",i,j);
    j--;
}

不会工作并且

int i=0,j=10;
while(i++,j>0){
    printf("%d%d",i,j);
    j--;
}

有效。

也请告诉我为什么

int i=0,j=10;
while(j>0,++i){
    printf("%d%d",i,j);
    j--;
}

给出一个无限循环?

感谢和问候

戒色

最佳答案

在您的 while 循环条件中,您使用逗号运算符,它计算其参数并返回第二个参数。所以在你的例子中:

while(j>0,i++) - returns i before it gets incremented; 
                 that is 0, so loop won't execute at all 

while(i++,j>0) - returns (j > 0) - runs as expected

while(j>0,++i) - returns i - will run until i overflows  max int value

关于c while循环入口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3390239/

相关文章:

c - 赋值 <指向常量数组的指针> = <指向数组的指针> : incompatible pointers

c++ - 像访问文件流一样访问一 block 内存(/C/C++数组)

c - 未初始化的 C 结构字段

python - while 循环删除数据存储中的数据

c++ - 用 while 循环绘制一个简单的三角形

用 C 代码创建 GPA 计算器

c - 在 sigmoid 传递函数中用整数替换 float 学

java - While 循环是否比 For 循环更好?

c - Arduino代码编译错误: "lvalue required as left operand of assignment"

r - 对于R中的大型必要循环,应优先使用 “while loops”而不是 “for loops”吗?