“for(;;)”在C#中做了什么?

时间:2022-11-15 22:33:42

I've seen this on occasion in books I've read. But I've found no explanation.

我有时会在读过的书中看到这个。但我没有找到任何解释。

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

Is it kind of like "while(true)"? Basically an endless loop for polling or something? Basically something you'd do until you intentionally break the loop?

它有点像“while(true)”吗?基本上是一个无限循环的轮询或什么?基本上你会做什么,直到你故意打破循环?

17 个解决方案

#1


49  

Is it kind of like "while(true)"?

它有点像“while(true)”吗?

Yes. It loops forever.

是。它永远循环。


Also note the comment by Andrew Coleson:

另请注意Andrew Coleson的评论:

Languages like C don't have built-in boolean primitives, so some people prefer for(;;) over while(1)

像C这样的语言没有内置的布尔基元,所以有些人喜欢(;;)而不是(1)

#2


20  

Yes.

是。

In a for if nothing is provided:

如果没有提供任何内容:

  • The initialisation does nothing.
  • 初始化什么都不做。
  • The condition is always true
  • 条件总是如此
  • The count statement does nothing
  • count语句什么都不做

It is equivalent to while(true).

它相当于while(true)。

#3


17  

You are correct. This is a common C# idiom for an endless loop.

你是对的。这是无限循环的常见C#习语。

#4


13  

Correct. Note that the braces of a for loop contain three parts:

正确。请注意,for循环的大括号包含三个部分:

  1. Initialization code
  2. 初始化代码
  3. A condition for continuing the loop
  4. 继续循环的条件
  5. Something that gets executed for each loop iteration
  6. 为每个循环迭代执行的操作

With for(;;), all of these are empty, so there is nothing done to initialize the loop, there is no condition to keep it running (i.e. it will run indefinitely) and nothing that gets executed for each iteration except the loop's content.

对于for(;;),所有这些都是空的,所以没有做任何事情来初始化循环,没有条件让它保持运行(即它将无限期地运行)并且除了循环的内容之外没有为每次迭代执行任何操作。

#5


12  

Yes, It is an infinite loop.

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

#6


11  

If I recall correctly it's use over "while(true)", is it more resembles "for(;;) //ever"

如果我没记错它是用于“while(true)”,它是否更像“for(;;)// ever”

#7


10  

Yes, it's an endless loop, just like while(true).

是的,它是一个无限循环,就像while(真实)。

It's the slightly preferred convention, probably because it's shorter. There's no efficiency difference at all.

这是稍微偏好的惯例,可能是因为它更短。完全没有效率差异。

#8


9  

Loop forever.

永远循环。

#9


9  

Take a look at a for loop.

看看for循环。

for ( initialization ; condition ; increment  )

1) initialization - set a counter variable here
2) condition - keep looping until the counter variable meets the condition
3) increment - increment the counter

1)初始化 - 在这里设置计数器变量2)条件 - 保持循环直到计数器变量满足条件3)增量 - 增加计数器

If there is no condition, a loop will go on forever. If it does such, then there is no need for a counter. Therefore

如果没有条件,循环将永远持续下去。如果它这样做,则不需要计数器。因此

for(;;)

#10


8  

Yes, it's an infinite loop. Same idea/effect as doing while(true) { ... }

是的,这是一个无限循环。与while(true)相同的想法/效果{...}

#11


8  

Inifinite loop like saying

像说的无限循环

while (0<1)

#12


6  

To be precise, any for loop without anything between the semicolons will loop forever (until terminated by some other means), because it has no defined invariant.

确切地说,分号之间没有任何东西的任何for循环将永远循环(直到通过其他方式终止),因为它没有定义的不变量。

#13


4  

It doesn't have an end condition, so it will loop forever until it find a break, as you already guessed.

它没有结束条件,因此它会永远循环,直到找到休息时间,正如您已经猜到的那样。

#14


4  

I might also add that it looks like 2 smiley faces winking at you

我还可以补充说,它看起来像两个笑脸对你眨眼

for (; ;)

for(;;)

maybe that's why some people like to use it.

也许这就是有些人喜欢使用它的原因。

#15


4  

Yes, it loops forever. But the reason why you should use

是的,它永远循环。但你应该使用的原因

for(;;)

instead of

代替

while(true)

is that

就是它

while(true)

will give you a compiler warning "conditional expression constant", while the for-loop does not. At least you'll get such a compiler warning in the highest warning level.

会给你一个编译器警告“条件表达式常量”,而for循环不会。至少你会在最高警告级别获得这样的编译器警告。

#16


2  

Yes! .

是! 。

#17


1  

Often used in embedded programming.

常用于嵌入式编程。

-setup interrupts and timers. -then loop forever.

- 设置中断和定时器。 - 然后永远循环。

When an interrupt or timer occurs that will be handled.

当发生将要处理的中断或定时器时。

#1


49  

Is it kind of like "while(true)"?

它有点像“while(true)”吗?

Yes. It loops forever.

是。它永远循环。


Also note the comment by Andrew Coleson:

另请注意Andrew Coleson的评论:

Languages like C don't have built-in boolean primitives, so some people prefer for(;;) over while(1)

像C这样的语言没有内置的布尔基元,所以有些人喜欢(;;)而不是(1)

#2


20  

Yes.

是。

In a for if nothing is provided:

如果没有提供任何内容:

  • The initialisation does nothing.
  • 初始化什么都不做。
  • The condition is always true
  • 条件总是如此
  • The count statement does nothing
  • count语句什么都不做

It is equivalent to while(true).

它相当于while(true)。

#3


17  

You are correct. This is a common C# idiom for an endless loop.

你是对的。这是无限循环的常见C#习语。

#4


13  

Correct. Note that the braces of a for loop contain three parts:

正确。请注意,for循环的大括号包含三个部分:

  1. Initialization code
  2. 初始化代码
  3. A condition for continuing the loop
  4. 继续循环的条件
  5. Something that gets executed for each loop iteration
  6. 为每个循环迭代执行的操作

With for(;;), all of these are empty, so there is nothing done to initialize the loop, there is no condition to keep it running (i.e. it will run indefinitely) and nothing that gets executed for each iteration except the loop's content.

对于for(;;),所有这些都是空的,所以没有做任何事情来初始化循环,没有条件让它保持运行(即它将无限期地运行)并且除了循环的内容之外没有为每次迭代执行任何操作。

#5


12  

Yes, It is an infinite loop.

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

#6


11  

If I recall correctly it's use over "while(true)", is it more resembles "for(;;) //ever"

如果我没记错它是用于“while(true)”,它是否更像“for(;;)// ever”

#7


10  

Yes, it's an endless loop, just like while(true).

是的,它是一个无限循环,就像while(真实)。

It's the slightly preferred convention, probably because it's shorter. There's no efficiency difference at all.

这是稍微偏好的惯例,可能是因为它更短。完全没有效率差异。

#8


9  

Loop forever.

永远循环。

#9


9  

Take a look at a for loop.

看看for循环。

for ( initialization ; condition ; increment  )

1) initialization - set a counter variable here
2) condition - keep looping until the counter variable meets the condition
3) increment - increment the counter

1)初始化 - 在这里设置计数器变量2)条件 - 保持循环直到计数器变量满足条件3)增量 - 增加计数器

If there is no condition, a loop will go on forever. If it does such, then there is no need for a counter. Therefore

如果没有条件,循环将永远持续下去。如果它这样做,则不需要计数器。因此

for(;;)

#10


8  

Yes, it's an infinite loop. Same idea/effect as doing while(true) { ... }

是的,这是一个无限循环。与while(true)相同的想法/效果{...}

#11


8  

Inifinite loop like saying

像说的无限循环

while (0<1)

#12


6  

To be precise, any for loop without anything between the semicolons will loop forever (until terminated by some other means), because it has no defined invariant.

确切地说,分号之间没有任何东西的任何for循环将永远循环(直到通过其他方式终止),因为它没有定义的不变量。

#13


4  

It doesn't have an end condition, so it will loop forever until it find a break, as you already guessed.

它没有结束条件,因此它会永远循环,直到找到休息时间,正如您已经猜到的那样。

#14


4  

I might also add that it looks like 2 smiley faces winking at you

我还可以补充说,它看起来像两个笑脸对你眨眼

for (; ;)

for(;;)

maybe that's why some people like to use it.

也许这就是有些人喜欢使用它的原因。

#15


4  

Yes, it loops forever. But the reason why you should use

是的,它永远循环。但你应该使用的原因

for(;;)

instead of

代替

while(true)

is that

就是它

while(true)

will give you a compiler warning "conditional expression constant", while the for-loop does not. At least you'll get such a compiler warning in the highest warning level.

会给你一个编译器警告“条件表达式常量”,而for循环不会。至少你会在最高警告级别获得这样的编译器警告。

#16


2  

Yes! .

是! 。

#17


1  

Often used in embedded programming.

常用于嵌入式编程。

-setup interrupts and timers. -then loop forever.

- 设置中断和定时器。 - 然后永远循环。

When an interrupt or timer occurs that will be handled.

当发生将要处理的中断或定时器时。