for(; true;)与while(true)不同?

时间:2022-08-21 23:55:09

If my understanding is correct, they do exactly the same thing. Why would anyone use for the "for" variant? Is it just taste?

如果我的理解是正确的,他们会做同样的事情。为什么有人会使用“for”变体?这只是味道吗?

Edit: I suppose I was also thinking of for (;;).

编辑:我想我也在考虑(;;)。

8 个解决方案

#1


40  

for (;;)

is often used to prevent a compiler warning:

通常用于防止编译器警告:

while(1)

or

while(true)

usually throws a compiler warning about a conditional expression being constant (at least at the highest warning level).

通常会抛出编译器警告条件表达式是否为常量(至少在最高警告级别)。

#2


8  

Yes, it is just taste.

是的,这只是味道。

#3


6  

I've never seen for (;true;). I have seen for (;;), and the only difference seems to be one of taste. I've found that C programmers slightly prefer for (;;) over while (1), but it's still just preference.

我从未见过(;是的)。我见过(;;),唯一的区别似乎是味道。我发现C程序员稍微喜欢(;;)而不是(1),但它仍然只是偏好。

#4


4  

Not an answer but a note: Sometimes remembering that for(;x;) is identical to while(x) (In other words, just saying "while" as I examine the center expression of an if conditional) helps me analyze nasty for statements...
For instance, it makes it obvious that the center expression is always evaluated at the beginning of the first pass of the loop, something you may forget, but is completely unambiguous when you look at it in the while() format.

不是一个答案,而是一个注释:有时记住for(; x;)与while(x)相同(换句话说,只是说“while”,当我检查if条件的中心表达式时)帮助我分析讨厌的语句...例如,很明显中心表达式总是在循环的第一遍开始时进行评估,这可能会让你忘记,但是当你以while()格式查看时,它是完全明确的。

Sometimes it also comes in handy to remember that

有时记住它也会派上用场

a;
while(b) {
   ...
   c;
}

is almost (see comments) the same as

几乎(见评论)相同

for(a;b;c) {
    ...
}

I know it's obvious, but being actively aware of this relationship really helps you to quickly convert between one form and the other to clarify confusing code.

我知道这很明显,但是积极地意识到这种关系确实可以帮助你快速地在一种形式和另一种形式之间进行转换,以澄清令人困惑的代码。

#5


3  

Some compilers (with warnings turned all the way up) will complain that while(true) is a conditional statement that can never fail, whereas they are happy with for (;;).

一些编译器(警告一直向上)将抱怨虽然(true)是一个永远不会失败的条件语句,而他们对(;;)感到满意。

For this reason I prefer the use of for (;;) as the infinite loop idiom, but don't think it is a big deal.

出于这个原因,我更喜欢使用for(;;)作为无限循环习语,但不要认为这是一个大问题。

#6


1  

It's in case they plan to use a real for() loop later. If you see for(;true;), it's probably code meant to be debugged.

如果他们计划稍后使用真正的for()循环。如果你看到(; true;),它可能是要调试的代码。

#7


0  

An optimizing compiler should generate the same assembly for both of them -- an infinite loop.

优化编译器应为它们生成相同的程序集 - 无限循环。

#8


0  

The compiler warning has already been discussed, so I'll approach it from a semantics stand-point. I use while(TRUE) rather than for(;;) because in my mind, while(TRUE) sounds like it makes more sense than for(;;). I read while(TRUE) as "while TRUE is always TRUE". Personally, this is an improvement in the readability of code.

编译器警告已经讨论过,所以我将从语义角度来看待它。我使用while(TRUE)而不是(;;),因为在我看来,(TRUE)听起来比((;;)更有意义)。我读取(TRUE)为“TRUE始终为TRUE”。就个人而言,这是对代码可读性的改进。

So, Zeus forbid I don't document my code (this -NEVER- happens, of course) it stays just a little bit more readable than the alternative.

所以,Zeus禁止我不记录我的代码(当然,这也就是发生)它比替代方案更容易读取。

But, overall, this is such a nit-picky thing that it comes down to personal preference 99% of the time.

但是,总的来说,这是一个非常挑剔的事情,99%的情况下归结为个人偏好。

#1


40  

for (;;)

is often used to prevent a compiler warning:

通常用于防止编译器警告:

while(1)

or

while(true)

usually throws a compiler warning about a conditional expression being constant (at least at the highest warning level).

通常会抛出编译器警告条件表达式是否为常量(至少在最高警告级别)。

#2


8  

Yes, it is just taste.

是的,这只是味道。

#3


6  

I've never seen for (;true;). I have seen for (;;), and the only difference seems to be one of taste. I've found that C programmers slightly prefer for (;;) over while (1), but it's still just preference.

我从未见过(;是的)。我见过(;;),唯一的区别似乎是味道。我发现C程序员稍微喜欢(;;)而不是(1),但它仍然只是偏好。

#4


4  

Not an answer but a note: Sometimes remembering that for(;x;) is identical to while(x) (In other words, just saying "while" as I examine the center expression of an if conditional) helps me analyze nasty for statements...
For instance, it makes it obvious that the center expression is always evaluated at the beginning of the first pass of the loop, something you may forget, but is completely unambiguous when you look at it in the while() format.

不是一个答案,而是一个注释:有时记住for(; x;)与while(x)相同(换句话说,只是说“while”,当我检查if条件的中心表达式时)帮助我分析讨厌的语句...例如,很明显中心表达式总是在循环的第一遍开始时进行评估,这可能会让你忘记,但是当你以while()格式查看时,它是完全明确的。

Sometimes it also comes in handy to remember that

有时记住它也会派上用场

a;
while(b) {
   ...
   c;
}

is almost (see comments) the same as

几乎(见评论)相同

for(a;b;c) {
    ...
}

I know it's obvious, but being actively aware of this relationship really helps you to quickly convert between one form and the other to clarify confusing code.

我知道这很明显,但是积极地意识到这种关系确实可以帮助你快速地在一种形式和另一种形式之间进行转换,以澄清令人困惑的代码。

#5


3  

Some compilers (with warnings turned all the way up) will complain that while(true) is a conditional statement that can never fail, whereas they are happy with for (;;).

一些编译器(警告一直向上)将抱怨虽然(true)是一个永远不会失败的条件语句,而他们对(;;)感到满意。

For this reason I prefer the use of for (;;) as the infinite loop idiom, but don't think it is a big deal.

出于这个原因,我更喜欢使用for(;;)作为无限循环习语,但不要认为这是一个大问题。

#6


1  

It's in case they plan to use a real for() loop later. If you see for(;true;), it's probably code meant to be debugged.

如果他们计划稍后使用真正的for()循环。如果你看到(; true;),它可能是要调试的代码。

#7


0  

An optimizing compiler should generate the same assembly for both of them -- an infinite loop.

优化编译器应为它们生成相同的程序集 - 无限循环。

#8


0  

The compiler warning has already been discussed, so I'll approach it from a semantics stand-point. I use while(TRUE) rather than for(;;) because in my mind, while(TRUE) sounds like it makes more sense than for(;;). I read while(TRUE) as "while TRUE is always TRUE". Personally, this is an improvement in the readability of code.

编译器警告已经讨论过,所以我将从语义角度来看待它。我使用while(TRUE)而不是(;;),因为在我看来,(TRUE)听起来比((;;)更有意义)。我读取(TRUE)为“TRUE始终为TRUE”。就个人而言,这是对代码可读性的改进。

So, Zeus forbid I don't document my code (this -NEVER- happens, of course) it stays just a little bit more readable than the alternative.

所以,Zeus禁止我不记录我的代码(当然,这也就是发生)它比替代方案更容易读取。

But, overall, this is such a nit-picky thing that it comes down to personal preference 99% of the time.

但是,总的来说,这是一个非常挑剔的事情,99%的情况下归结为个人偏好。