[英]What does for(;1;) mean in the C language?
What does for(;1;)
mean in C? for(;1;)
在 C 中是什么意思?
Is it the same as for(;;)
in the sense of an infinite loop?在无限循环的意义上,它是否与
for(;;)
相同?
I know that the position where 1
is typed is the condition part of the variable, but in this for
loop, I'm not convinced that it is indeed an infinite loop.我知道输入
1
的位置是变量的条件部分,但在这个for
循环中,我不相信它确实是一个无限循环。
According to the C Standard (6.8.5.3 The for statement)根据 C 标准(6.8.5.3 for 语句)
2 Both clause-1 and expression-3 can be omitted.
2 子句 1 和表达式 3 都可以省略。 An omitted expression-2 is replaced by a nonzero constant.
省略的表达式 2 由非零常量替换。
So in fact you yourself instead of the compiler replaced the omitted expression-2 by nonzero constant 1.所以实际上你自己而不是编译器用非零常量 1 替换了省略的表达式 2。
So these two statements所以这两个陈述
for(;1;) { /*...*/ }
and和
for(;;) { /*...*/ }
are fully equivalent.是完全等价的。
In my opinion it is much better to write在我看来,写得更好
while ( 1 ) { /*...*/ }
instead of the shown above for statements.而不是上面显示的语句。
Be convinced.被说服。 For loop works until the condition-part of the expression becomes false.
For 循环一直工作,直到表达式的条件部分变为假。 And in
C
1 means " true
", which - obviously - will never become false.而在
C
1 的意思是“ true
”,很明显,它永远不会变成假。
Basically, Your both expressions for(;1;) and for(;;) will give you infinite loop.基本上,你的两个表达式 for(;1;) 和 for(;;) 都会给你无限循环。
All of the expressions of the for statement are optional;
for 语句的所有表达式都是可选的; for example, the following statement is used to write an infinite loop:
例如,以下语句用于编写无限循环:
for (; ; )
{
// ...
}
So no criteria in condition of loop will give you a infinite loop.所以循环条件中的任何条件都不会给你一个无限循环。
On you other question for(;1;): you are not convinced that it will give you infinite loop.关于你的另一个问题 for(;1;): 你不相信它会给你无限循环。
It has to a infinite loop, Because result of condition statement is not equal to 0.它必须无限循环,因为条件语句的结果不等于 0。
Following are some examples:以下是一些示例:
for(;1;) //Infinite loop
for(;0;) //Loop will not execute at all
for(;-1;) //Infinite loop.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.