在C#是为(;;)安全?它真的? [英] In C# is a for(;;) safe and what does it really do?
问题描述
我发现了一个空的for语句在code现有的一点,我不知道它做什么,是它的安全。它只是感觉错了。
I found an empty for statement in an existing bit of code and I'm wondering what it does and is it "safe". It just feels wrong.
for(;;)
{
//some if statements and a case statement
}
谢谢!
推荐答案
这是创造一个无限循环的一种方式。这是一个普通的为
循环,但空的初始化,条件和增量EX pressions。由于条件EX pression是一个空操作,该循环永远不会退出。这是完全安全的假设它有一个终止条件(A 破发
或返回
语句[甚至 GOTO
,我想])的地方。
This is one way of creating an infinite loop. It's a regular for
loop, but with empty initialization, condition, and increment expressions. Because the condition expression is a no-op, the loop never exits. It's perfectly "safe" assuming it has a terminating condition (a break
or return
statement [or even a goto
, I suppose]) somewhere.
我个人preFER写与无限循环,而
S:
Personally, I prefer to write infinite loops with while
s:
while (true)
{
//some statements and a case statement
}
(因为为
是迭代和,而
是重复的)。
不过,看完<后href="http://stackoverflow.com/questions/2611246/is-for-faster-than-while-true-if-not-why-do-people-use-it/2611529">this问题(由@jailf链接),我现在preFER ,而(42){...}
。
However, after reading this question (linked by @jailf), I now prefer while (42) { ... }
.
这篇关于在C#是为(;;)安全?它真的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!