简体   繁体   English

c循环输入

[英]c while loop entry

somebody please explain me why 有人请解释我原因

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

will not work and 不会工作

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

works. 作品。

Also please tell me why 还请告诉我原因

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

gives an infinite loop ? 给出一个无限循环?

thanks and regards 谢谢并恭祝安康

harsha

In your while-loop condition you use comma operator, which evaluates its parameters and returns the second one. 在while循环条件中,使用逗号运算符,它会计算其参数并返回第二个参数。 So in your examples: 所以在你的例子中:

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

Read up on the C comma operator . 阅读C 逗号运算符 Basically it winds down to the fact that the comma operator returns the result of whatever is on the right side of the comma - so in your first example, i++ returns 0, and the loop ends. 基本上它归结为逗号运算符返回逗号右侧的结果 - 所以在第一个例子中,i ++返回0,循环结束。 In the third case, ++i is never 0 (at least not for a long time) so you get the infinite loop. 在第三种情况下,++ i永远不会为0(至少不会持续很长时间),因此您将获得无限循环。 The middle case is ok, since the result of j>0 is returned from the comma operator, and your loop works as expected. 中间的情况是可以的,因为j>0的结果是从逗号运算符返回的,并且你的循环按预期工作。

You're using the comma operator. 您正在使用逗号运算符。 The result of the comma operator is the second sub-expression. 逗号运算符的结果是第二个子表达式。 So j>0,i++ evaluates to i++ . 所以j>0,i++评估为i++ i++ is initially 0, so the loop never executes. i++最初为0,因此循环永远不会执行。

Likewise, j>0,++i evaluates to ++i , which will be non-zero until you overflow, so it appears to loop forever (though really just for a long time). 同样, j>0,++i计算到++i ,在你溢出之前它将是非零的,所以它似乎永远循环(虽然真的只是很长一段时间)。

i++,j>0 works because the last-subexpression is j>0 , which is the actual condition that you want. i++,j>0有效,因为last-subexpression是j>0 ,这是你想要的实际条件。 Note that even though the comma operator throws away the result of the first expression ( i++ , in this case) it still evaluates that sub-expression, and so you still get the side-effect (incrementing i ). 请注意,即使逗号运算符抛弃了第一个表达式的结果(在这种情况下为i++ ),它仍然会计算该子表达式,因此您仍然会得到副作用(递增i )。

In these cases, you have expressions that include a comma operator. 在这些情况下,您有包含逗号运算符的表达式。 The comma operator evaluates its left operand, then its right operand. 逗号运算符计算其左操作数,然后计算其右操作数。 The result is that of the right operand. 结果是右操作数的结果。

For the moment, let's consider only your last question: 目前,让我们只考虑你的最后一个问题:

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

This should not really be an infinite loop, though it may run just a bit longer than expected. 这不应该真正无限循环,虽然它可能运行只是有点比预期更长。 Since the result of the comma operator is the result of the right operand, this is waiting for ++i to become 0. Since it's starting at 0, it's going to cycle though all possible values of an int before it terminates. 由于逗号运算符的结果是操作数的结果,因此等待++i变为0.因为它从0开始,它将在终止之前循环通过int 所有可能值。 With a 16-bit int , that would probably take a few minutes. 使用16位int ,可能需要几分钟。 With a 32-bit int , it's going to take quite a bit longer. 随着32位int ,这将非常需要更长的时间。 With a 64-bit int , it would be an infinite loop for any practical purpose -- I'm pretty sure I'll die long before it could hope to finish at any rate... 使用64位int ,对于任何实际目的来说都是无限循环 - 我很确定我会在它希望以任何速度完成之前很久就会死掉......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM