什么是“for(;;)”循环用于? [重复]

时间:2022-11-26 10:48:27

Possible Duplicate:
Infinite loop application - for(;;)

可能重复:无限循环应用程序 - for(;;)

I am a Java developer, and I saw this in one of the classes:

我是一名Java开发人员,我在其中一个类中看到了这个:

for(;;){
something goes here
}

What does this loop mean? When would we have to use it?

这个循环是什么意思?我们何时才能使用它?

Thanks

谢谢

5 个解决方案

#1


11  

It's an infinite loop, an equivalent of this could be

这是一个无限循环,相当于这个

while(true) which is definitely used a lot more than an infinite for loop.

而(true)肯定比无限循环使用更多。

But they both mean the same thing in this situation.

但在这种情况下,它们都意味着同样的事情。

#2


5  

It's an infinite loop. Similar to while(true) { ... } or do { ... } while(true);. A reason you might use this is if in the something goes here part, you have a break; with a complex condition.

这是一个无限循环。类似于while(true){...}或do {...} while(true);.你可能会使用这个的一个原因是,如果在这里的某些部分,你有一个休息;条件复杂。

For simple break conditions, it's easier to just put them in the loop statement.

对于简单的中断条件,将它们放在循环语句中更容易。

#3


2  

It's the same as

它是一样的

while(true)

However it's less used and in general should be avoided.

然而,它的使用较少,一般应避免使用。

#4


2  

It's an infinite loop, equivalent to more readable while(true).

它是一个无限循环,相当于更可读的while(true)。

Such a loop is not used often, it typically represents operations that should continue running as long as the application runs. E.g:

这种循环不经常使用,它通常表示只要应用程序运行就应该继续运行的操作。例如:

  • server thread accepting connections

    服务器线程接受连接

  • cleanup process sleeping for some time

    清理过程睡了一段时间

  • reading infinite input with pauses

    通过暂停读取无限输入

The only way to escape such a loop is:

逃避这种循环的唯一方法是:

  • break

    打破

  • throwing an exception

    抛出异常

  • interrupting a thread (equivalent to throwing an InterruptedException inside a loop)

    中断线程(相当于在循环中抛出InterruptedException)

#5


1  

This is an infinite loop. The idea is that a break condition is inside the loop

这是一个无限循环。想法是循环内部有一个中断条件

int i=0;
for(;;)
{
    i++;
    if (i>10) break;
}

#1


11  

It's an infinite loop, an equivalent of this could be

这是一个无限循环,相当于这个

while(true) which is definitely used a lot more than an infinite for loop.

而(true)肯定比无限循环使用更多。

But they both mean the same thing in this situation.

但在这种情况下,它们都意味着同样的事情。

#2


5  

It's an infinite loop. Similar to while(true) { ... } or do { ... } while(true);. A reason you might use this is if in the something goes here part, you have a break; with a complex condition.

这是一个无限循环。类似于while(true){...}或do {...} while(true);.你可能会使用这个的一个原因是,如果在这里的某些部分,你有一个休息;条件复杂。

For simple break conditions, it's easier to just put them in the loop statement.

对于简单的中断条件,将它们放在循环语句中更容易。

#3


2  

It's the same as

它是一样的

while(true)

However it's less used and in general should be avoided.

然而,它的使用较少,一般应避免使用。

#4


2  

It's an infinite loop, equivalent to more readable while(true).

它是一个无限循环,相当于更可读的while(true)。

Such a loop is not used often, it typically represents operations that should continue running as long as the application runs. E.g:

这种循环不经常使用,它通常表示只要应用程序运行就应该继续运行的操作。例如:

  • server thread accepting connections

    服务器线程接受连接

  • cleanup process sleeping for some time

    清理过程睡了一段时间

  • reading infinite input with pauses

    通过暂停读取无限输入

The only way to escape such a loop is:

逃避这种循环的唯一方法是:

  • break

    打破

  • throwing an exception

    抛出异常

  • interrupting a thread (equivalent to throwing an InterruptedException inside a loop)

    中断线程(相当于在循环中抛出InterruptedException)

#5


1  

This is an infinite loop. The idea is that a break condition is inside the loop

这是一个无限循环。想法是循环内部有一个中断条件

int i=0;
for(;;)
{
    i++;
    if (i>10) break;
}