Joh*_*ohn 6 c++ loops for-loop while-loop
我看到了我读过的许多书中使用的不同约定,你可以在其中创建具有循环结构的无限循环,例如:
while()
foo();
for(;;)
foo();
Run Code Online (Sandbox Code Playgroud)
但实际上,我应该知道的差异是什么?哪一个更好?
它们在语义上是等价的.(x;y;z) { foo; }相当于x; while (y) { foo; z; }.它们在标准的其他版本中并不完全等效,在示例中for (int x = 0; y; z),范围x是for block并且在循环结束后超出范围,而int x; while (y) x在循环结束后仍然在范围内.
另一个区别是for将缺失解释y为TRUE,而while必须提供表达式.for (;;) { foo; }很好,但while() { foo; }不是.
| 归档时间: |
|
| 查看次数: |
1823 次 |
| 最近记录: |