Why choosing for (;;){} over while(1)?(为什么选择 (;;){} 而不是 while(1)?)
问题描述
可能的重复:
是for(;;)"比while (TRUE)"快?如果不是,人们为什么要使用它?
我看到了
for (;;)
{
// Some code here
}
经常出现.但它提供了什么好处,为什么不选择 while(1){}?
quite often. But what benefits does it offer and why just not choose while(1){}?
推荐答案
它们产生相同的代码.您可能更喜欢 for (;;) 的原因有很多,但这只是个人喜好:
They produce identical code. There are a couple of reasons why you might prefer for (;;) but it is all just personal preference:
有些编译器会警告您有关始终为真的条件.
for(;;)不会有这个问题.
for (;;) 字面意思是永远循环下去!",而 while (true) 仍然出现有某种条件.
for (;;) literally reads as "Just loop forever!", whereas while (true) still appears to have some kind of condition.
我说选择一个并坚持下去.只要不随意切换都无所谓.
I say pick one and stick with it. It doesn't matter as long as you don't switch between them arbitrarily.
这篇关于为什么选择 (;;){} 而不是 while(1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么选择 (;;){} 而不是 while(1)?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
