What is the for (;;) loop used for?

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

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

for(;;){ something goes here } 

What does this cycle mean? When will we use it?

thanks

+4
source share
5 answers

This is an infinite loop, the equivalent of which may be

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

But in this situation, they both mean the same thing.

+11
source

This is an endless cycle. Similar to while(true) { ... } or do { ... } while(true); . The reason you can use this is because in the something goes here , you have a break; with a difficult condition.

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

+5
source

This is the same as

 while(true) 

However, it is less used and should generally be avoided.

+2
source

This is an infinite loop, equivalent to a more readable while(true) .

Such a loop is often not used; it usually represents operations that should continue until the application is running. For instance:

  • server thread accepting connections

  • cleaning process for some time

  • reading endless input with pauses

The only way to avoid such a loop is:

  • break

  • reset exception

  • thread interruption (equivalent to throwing an InterruptedException inside the loop)

+2
source

This is an endless cycle. The idea is that the interrupt condition is inside the loop

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

Source: https://habr.com/ru/post/1445875/


All Articles