-5

What is the meaning of for (;;)?

I found it in a certain base class and I cannot find explanation for in on the net.

Please also explain when and how to use this expression

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • 4
    Why downvotes if this is the first time I am seeing this expression? Enter in google term "java for (;;)" and see if you will find answer to this. – sandalone Sep 21 '13 at 17:20
  • 1
    Not downvoter, but I've searched *java empty for* and found http://stackoverflow.com/q/7081339/1065197 – Luiggi Mendoza Sep 21 '13 at 17:21
  • Run it and wait till you see the result! You don't need to search the web for everything – Afshin Moazami Sep 21 '13 at 17:22
  • 1
    you can write this code and can easily see what happens. – erencan Sep 21 '13 at 17:22
  • 3
    Honestly? I have just Google it and this is the first result: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html I guess it needs downvote, since it duplicated question not only in stackoverflow, but also in all the Java tutorials and books! – Afshin Moazami Sep 21 '13 at 17:23
  • Thanks guys. I am aware of infinite loops but have never seen such usage. – sandalone Sep 21 '13 at 17:24
  • 1
    It is like saying that, I know what is c=c+1, but I have never seen c++ in c++ language! – Afshin Moazami Sep 21 '13 at 17:26

4 Answers4

10

It's an infinite loop. It has no initial condition, no increment and no end condition.

It's the same as

while(true) {
    //do stuff
}

You can use it when you need something to be repeated continuously, until the application ends. But i would use the while version instead

BackSlash
  • 21,927
  • 22
  • 96
  • 136
2

It is read "for ever". the default for the condition is to evaluate to true.

When should you use it? If you don't want the loop to end (as is common in servers) or when the end condition is naturally known only in the middle of the loop, in which case you can use break:

for (;;) {
    String in = get_input();
    if (in.equals("end"))
        break;
    System.out.println("you entered " + in);
}

This is not the best example, though. You can do without the infinite loop here. For example:

for (String in = get_input(); !in.equals("end"); in = get_input()) {
    System.out.println("you entered " + in);
}
Elazar
  • 20,415
  • 4
  • 46
  • 67
1

It means an infinite loop. It is not considered a good practice to use this. Instead try while(true)

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I can't see the difference between `while(true)` and `for (;;)`. – Elazar Sep 21 '13 at 17:25
  • @Elazar:- There is as such no difference between the two but while(true) is considered a better approach. Do correct me if I am wrong :) – Rahul Tripathi Sep 21 '13 at 17:27
  • @Elazar:- Also to add to my point there is no performance difference between the usage of the two but still it is considered a good practice to use while(true) instead of for(;;) for infinite loop :) – Rahul Tripathi Sep 21 '13 at 17:29
  • 1
    I believe the choice between them is only a matter of consistency with the rest of the code, and even then it is not important.. – Elazar Sep 21 '13 at 17:30
  • @Elazar:- Yes I agree. +1 :) – Rahul Tripathi Sep 21 '13 at 17:31
1

In some cases where you want to do a task again and again, sometimes forever. Such cases you use an infinite loop. This can be done in many ways. Notable ones are,
Using while,

while(true)
{
   // Task you want to repeat
}

Using for,

for(;;)
{
      // Task you want to repeat
}

Note : You might have to be very much careful while implementing infinite loops as there exists a possibility of same problem recurring. It is a good practice to catch the exception and retry for some time and if still problem persist, break from the loop.

prasanth
  • 3,502
  • 4
  • 28
  • 42