Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What is "for (;;)" [duplicate]

Tags:

java

Would any one please explain this instruction for me: for (;;)

I have encountered several kinds of these mark (like in ajax code of facebook and in concurrent stuff of Java).

like image 983
Phương Nguyễn Avatar asked May 22 '10 14:05

Phương Nguyễn


People also ask

What is duplicate in Java?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

Can list have duplicate in Java?

List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value. List permits any number of null values in its collection while Set permits only one null value in its collection.


3 Answers

An infinite loop.

Each of the three parts of a for loop (for(x; y; z)) is optional.

So you can do this:

int i = 0;
for (; i < 20; ++i)

and it's perfectly valid, or

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

or

for (int i = 0; ; ++i) { if (i < 20) { break; } }

and they're all valid.

You can also omit all three parts, with for(;;). Then you have a loop that:

  • does no initialization (the first part)
  • has no condition for stopping (the middle part)
  • does nothing after each iteration (the last part)

so basically an endless loop. It just does what it says in the loop body, again and again

like image 73
jalf Avatar answered Oct 29 '22 16:10

jalf


It's an endless loop. For the specificion of the for statement, see here.

like image 38
tangens Avatar answered Oct 29 '22 14:10

tangens


That's an infinite loop, similar to

while(true)
{
    ...
}
like image 22
Bill the Lizard Avatar answered Oct 29 '22 16:10

Bill the Lizard