Two semicolons inside a for-loop parentheses

for(;;) {
}

functionally means

 while (true) {
 }

It will probably break the loop/ return from loop based on some condition inside the loop body.

The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:

for(i = 0; i < 10; i++)

If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;)‘, which (as shown above) is the same as while (true).

Leave a Comment