Why is for(;;) used?

Viewed 776

I have seen code (in C) which contains something similar to:

for(;;){
}

How would this work, and why is it used in any instance?

9 Answers

It is the idiomatic way to have an infinite loop.

In the C Programming Language book, by Kernighan & Ritchie book it was introduced in section 3.5:

for (;;) {
    ...
 }

is an infinite loop, presumably to be broken by other means, such as a break or return.

is an infinite loop something like

while(true) 
{}

This is equivalent to an infinite loop, as many other have explained. However, few of them explained why this executes as an infinite loop.

A for loop can be broken down into three parts:

for(initialization(s); condition(s); looping command(s))

None of these fields are actually required. Without a condition provided, there's nothing to stop the command from running. This is because the for loop looks for a false statement. Without conditions provided, nothing is false, therefore the loop runs indefinitely.

Therefore to cause a for loop to be infinite, all you need is to not provide a condition. This means that this is also a valid infinite loop:

for(int i = 0;; i++)
    printf("Iteration: %i\n", i);

For readability, and to make sure that the second semi-colon isn't a typo, some programmers might put a space between them, or put true as the condition.

Honestly, I prefer while(true), as this is a clear infinite loop. Using while(1) works as well, but '1' is an integer, not a boolean. While it is equivalent to true, it does not always mean true.

Between these three types of infinite loops, for(;;) has the fewest characters (7). while(1) comes second at 8 characters, and while(true) at 11.

I suppose that certain programmers prefer a low byte count over a readable program, but I wouldn't recommend using for(;;). While equivalent, I believe that using while(true) is better practice.

A for loop needs three expressions, which are separated by semicolons, and are completely optional:

  • An initialization (e.g. int i=0)
  • A condition (e.g. i < 10)
  • An afterthought (e.g. i++)

In this case, the three expressions are empty, and thus there's no condition that will make the loop stop, thus creating an infinite loop, unless a flow control instruction like break (which will exit the loop) or return is used.

Its an infinite loop. Its equivalent to:

while (true) {
}

The C# compiler directly translates for (; ;) into the exact same construct as while (true).

Infinite loop

same as

while(true){}

the code for(;;){} or while(true){} or while(1){} all represent infinite loops. An infinite loop is something to be expected in a software system that is expected to run and "unlimited" amount of time. Every OS has at least one - it's how a background task or idle task is implemented.

Real Time systems use infinite loops as well because the system has to handle events which are asynchronous;

Basically anything that runs software uses infinite loops in one way or another.

I don't know why no one answered why people do this instead of while(true): It's because while(true) will often generate a compilation warning that the condition of the loop is constant.

This kind of infinite loop can be used in microcontroleurs. In your main function, you initialize the basic functions of your microcontroleur, then put a while (1).

void MAIN(void)
{
  Init_Device();
  while(1);
}

The microcontroleur will then do nothing but wait for interrupts of internal or external devices (like a timer overflow, or UART data ready to be read), and react to these interrupts.

Related