对于(;;){}或while(true){}有什么好处?(What is better for(;;) {} or while(true) {}? [duplicate])

可能重复: “for(;;)”比“while(TRUE)”快吗? 如果没有,为什么人们会使用它?

我想知道哪个无限循环更可取:

for(;;) { // Do some stuff }

要么

#define TRUE 1 while(TRUE) { // Do some stuff }

从性能的角度来看,它们之间有什么区别吗?

从编码标准的角度来看,更可取的是什么?

Possible Duplicate: Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?

I would like to know which infinite loop is more preferable:

for(;;) { // Do some stuff }

or

#define TRUE 1 while(TRUE) { // Do some stuff }

Is there any difference between them from the performance point of view?

What is more preferable from coding standards point of view?

最满意答案

从性能的角度来看并不重要。 编译器将两者优化为相同的汇编代码。 从“编码标准”的角度来看,(;;)更简洁,这很好。 虽然while(TRUE)稍微清楚一点,但是(;;)是如此常见,以至于在这种情况下的清晰度并不是一个大问题(实际上,for(;;)的普遍性可能使它比while版本更清晰)。

From a performance point of view it doesn't matter. The compiler will optimize both into the same assembly code. From a "coding standards" point of view, for(;;) is more terse, which is nice. Although while(TRUE) is a bit clearer, for(;;) is so common that clarity in this case is not much of an issue (in fact, the ubiquity of for(;;) may make it more clear than the while version).

更多推荐