当所有表达式都缺失时,C#for循环会做什么。例如for(;;){}

时间:2022-10-07 22:29:38

I can only assume it's an infinite loop.

我只能假设它是一个无限循环。

Can I leave out any of the three expressions in a for loop? Is there a default for each when omitted?

我可以在for循环中省略三个表达式中的任何一个吗?省略时是否有默认值?

10 个解决方案

#1


It is indeed an infinite loop.

它确实是一个无限循环。

Under the hood the compiler/jitter will optimize this to (effectively) a simple JMP operation.

在引擎盖下,编译器/抖动将优化它以(有效地)简单的JMP操作。

It's also effectively the same as:

它实际上也是如此:

while (true)
{
}

(except that this is also optimized away, since the (true) part of a while expression usually requires some sort of comparison, however in this case, there's nothing to compare. Just keep on looping!)

(除了这个也被优化掉之外,因为while表达式的(真)部分通常需要某种比较,但是在这种情况下,没有什么可比较的。只需继续循环!)

#2


Yes, it's an infinite loop.

是的,这是一个无限循环。

Examples:

for (; ;) { } (aka: The Crab)

for(;;){}(又名:螃蟹)

while (true) { }

而(真){}

do { } while (true)

做{} while(true)

#3


Yes, that's an infinite loop. You can leave out any of the three expressions, though in my experience it's typically either the first or all 3.

是的,这是一个无限循环。您可以省略这三个表达式中的任何一个,但根据我的经验,它通常是第一个或全部3个。

#4


It's an infinitely loop. Effectively, its the same as this:

这是一个无限循环。实际上,它与此相同:

while (true)
{
}

#5


for (; ;) { } is infinite loop, you are correct.

for(;;){}是无限循环,你是对的。

if you want to use it, then you have to put some condition in your loop body, so that it can exit out from loop.

如果你想使用它,那么你必须在循环体中放置一些条件,以便它可以从循环中退出。

for (; ;) 
{
    if (conditionTrue)
        break;
    else
        continue;
} 

#6


There are no defaults. Nothing is initialised, nothing is incremented and there's no test for completion.

没有默认值。什么都没有初始化,没有任何增加,也没有完成测试。

#7


There are no defaults for first and third part (they default to nothing and it would work). The default for conditional expression is true which will make for(;;) effectively an infinite loop. (If the default was supposed to be false, it would have been useless to have such a construct).

第一和第三部分没有默认值(它们默认为空,它可以工作)。条件表达式的默认值为true,这将使(;;)实际上成为无限循环。 (如果默认值应该是假的,那么拥有这样的构造就没用了)。

#8


Just like in C and C++ you can omit any of the three, or all three.

就像在C和C ++中一样,你可以省略三个或全部三个中的任何一个。

#9


Microsoft's C# Programmer's Reference says:

微软的C#程序员参考文献说:

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

for语句的所有表达式都是可选的;例如,以下语句用于编写无限循环:

for (;;) {
   ...
}

#10


It's an infinite loop :).

这是一个无限循环:)。

#1


It is indeed an infinite loop.

它确实是一个无限循环。

Under the hood the compiler/jitter will optimize this to (effectively) a simple JMP operation.

在引擎盖下,编译器/抖动将优化它以(有效地)简单的JMP操作。

It's also effectively the same as:

它实际上也是如此:

while (true)
{
}

(except that this is also optimized away, since the (true) part of a while expression usually requires some sort of comparison, however in this case, there's nothing to compare. Just keep on looping!)

(除了这个也被优化掉之外,因为while表达式的(真)部分通常需要某种比较,但是在这种情况下,没有什么可比较的。只需继续循环!)

#2


Yes, it's an infinite loop.

是的,这是一个无限循环。

Examples:

for (; ;) { } (aka: The Crab)

for(;;){}(又名:螃蟹)

while (true) { }

而(真){}

do { } while (true)

做{} while(true)

#3


Yes, that's an infinite loop. You can leave out any of the three expressions, though in my experience it's typically either the first or all 3.

是的,这是一个无限循环。您可以省略这三个表达式中的任何一个,但根据我的经验,它通常是第一个或全部3个。

#4


It's an infinitely loop. Effectively, its the same as this:

这是一个无限循环。实际上,它与此相同:

while (true)
{
}

#5


for (; ;) { } is infinite loop, you are correct.

for(;;){}是无限循环,你是对的。

if you want to use it, then you have to put some condition in your loop body, so that it can exit out from loop.

如果你想使用它,那么你必须在循环体中放置一些条件,以便它可以从循环中退出。

for (; ;) 
{
    if (conditionTrue)
        break;
    else
        continue;
} 

#6


There are no defaults. Nothing is initialised, nothing is incremented and there's no test for completion.

没有默认值。什么都没有初始化,没有任何增加,也没有完成测试。

#7


There are no defaults for first and third part (they default to nothing and it would work). The default for conditional expression is true which will make for(;;) effectively an infinite loop. (If the default was supposed to be false, it would have been useless to have such a construct).

第一和第三部分没有默认值(它们默认为空,它可以工作)。条件表达式的默认值为true,这将使(;;)实际上成为无限循环。 (如果默认值应该是假的,那么拥有这样的构造就没用了)。

#8


Just like in C and C++ you can omit any of the three, or all three.

就像在C和C ++中一样,你可以省略三个或全部三个中的任何一个。

#9


Microsoft's C# Programmer's Reference says:

微软的C#程序员参考文献说:

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

for语句的所有表达式都是可选的;例如,以下语句用于编写无限循环:

for (;;) {
   ...
}

#10


It's an infinite loop :).

这是一个无限循环:)。