10

Though I have some experience in Java, the following code looks a bit strange to me:

public class ForLoopTest{
    public static void main(String[] args){
        for(;;){}
    } 
}

This code compiles fine, although the initialization-test-increment part is empty, unlike a usual for loop:

for(int i=0; i<10; i++){}

Since the code compiles fine, it's valid syntax.

Is there any practical use of this type of for loop where there is no initialization-test-increment part ?

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Razib
  • 10,965
  • 11
  • 53
  • 80
  • 2
    http://stackoverflow.com/questions/29414954/issues-with-clone-and-for-loop/29415184#29415184 This should answer your question :) – damus4 May 08 '15 at 13:41
  • 2
    Seeing this loop lets you detect coders with prior exposure to C. In particular, coders who read K&R would prefer this style of loops, even in Java and C#. – Sergey Kalinichenko May 08 '15 at 13:47
  • [Code golf?](http://codegolf.stackexchange.com/questions/tagged/code-golf) – TNT May 08 '15 at 15:03

7 Answers7

12

It's equivalent to while(true) {}.

There's no real reason to use or not to use a for loop that way (and it's not often that you would write a while(true) loop either).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Sure there is a real reason to use it: In code golf contests it's shorter and takes less characters :D – Benjamin Gruenbaum May 08 '15 at 15:02
  • There's a good reason: it is the standard idiom, the canonical way to write the "infinite loop" in C. It is mentioned in the Kernighan and Ritchie. There are many ways to have the same effect, but if you *don't* use `for(;;)`, someone reading your program will lose time searching the factual reason why you did so. And curse you because you didn't have any. – Michel Billaud May 08 '15 at 15:13
  • @MichelBillaud I'd say if someone is reading Java code and thinking of K & R, he's asking for trouble. – Kayaman May 10 '15 at 16:37
  • @Kayaman well, it started sort of a tradition in other languages with similar syntax. The first C++ or Java programmers were not absolute beginners learning their first language from scratch, they imported habits from the languages they already knew. Checking in "Java exemples in a nutshell" by David Flanagan (O'reilly 1997), the "loop, infinite" entry leads to an example of "for(;;)" p 64. – Michel Billaud May 10 '15 at 17:37
  • real reason to use : eventListener? – Milche Patern Jun 13 '15 at 05:38
9

This is one way of writing an infinite loop. It's equivalent to:

while(true){ }


Or:

boolean loop = true;
while (loop) {
    //do nothing
}

Or even:

int theAnswerToTheQuestionOfLifeTheUniverseAndEverything = 42;
while(theAnswerToTheQuestionOfLifeTheUniverseAndEverything == 42) { 
    //do nothing
} 

​​​​​​​​​​​​​​​​​​​​​​​​

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
7

It is an infinite loop. The way you can use it (without being an infinite loop) would be like this (you will probably never use this way):

int i = 0;
for(;;) {
  if(i == 10) { 
      break;
  }
  i++;
}
Alexander Jardim
  • 2,406
  • 1
  • 14
  • 22
Raphael Amoedo
  • 4,233
  • 4
  • 28
  • 37
4

Your loop for(;;){} is equivalent to while(true) {} Yes, it is an inifinite loop.

If your question is: There are any reason to use that? The answer is maybe yes.

I'll give you an example:

What if you want to do a program that read measurements from time to time continuously? like a monitor showing you what does a dron recording or something similar?

You will may need a loop like this to refresh the image.

But yes, usually you don't need that for anything.

Shondeslitch
  • 1,049
  • 1
  • 13
  • 26
2

Kayaman is correct. A loop in that format will need to have some exit condition within it, or it will undoubtedly be an infinite loop. Most people use while(true) in this case since it's easier to understand. I had a prof once tell me that the only advantage of for(;;) {} is that it looks more impressive than while(true).

Razib
  • 10,965
  • 11
  • 53
  • 80
2

As Kayaman also mentioned it's equivalent to while(true) {}.

One use case I can think of is - if you need to do some operation as soon as a semaphore file arrives in a directory. You can keep checking file arrival in an infinite loop, as soon as file arrives then do some operation and break the loop

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
2

for(;;) is equivalent to while(true). The reason you see the former more often in code is, that it is the canonical way of encoding an endless loop in the C programming language from where Java borrowed a lot of syntactical elements and which is the background of a lot of Java developers.

Using while(true) might look more straight-forward, but the C programming language had no boolean types nor true literal in the beginning, so the equivalent had to be while(1) then, which doesn’t look better than for(;;). It helped the latter to become a common pattern as it was specified by K&R (see this answer) as the idiom for an eternal loop.

Well, and changing the programming language doesn’t necessarily imply changing habits, so you see for(;;) in Java now almost as often as in C.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765