Why is used for (;;)?

I saw code (in C) that contains something similar to:

for(;;){ } 

How does it work and why is it used anyway?

+6
source share
9 answers

This is an idiomatic way to have an infinite loop.

In C Programming Language, Kernighan and Ritchie presented in Section 3.5:

 for (;;) { ... } 

represents an infinite loop, presumably, which must be broken in other ways, such as breaking or returning.

+20
source

- an endless loop similar to

 while(true) {} 
+8
source

This is equivalent to an endless loop, as many others have explained. However, some of them explained why this runs as an endless loop.

Cycle

A for can be broken into three parts:

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

None of these fields are required. Without the stipulated condition, there is nothing to stop the command from starting. This is because the for loop searches for the false operator. Without the provided conditions, nothing is false, so the loop runs indefinitely.

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

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

For ease of reading and so that the second half-bell is not a typo, some programmers could place a space between them or set true as a condition.

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

Between these three types of infinite loops, for(;;) has the least number of characters (7). while(1) takes second place with 8 characters, while while(true) takes 11.

I suggest that some programmers prefer low byte counting over the program being read, but I would not recommend using for(;;) . While equivalent, I believe that using while(true) is best practice.

+6
source
Cycle

A for requires three expressions, separated by semicolons, and completely optional:

  • Initialization (e.g. int i=0 )
  • Condition (e.g. i < 10 )
  • Subsequent thought (e.g. i++ )

In this case, the three expressions are empty, and therefore, there is no condition that stops the loop, thereby creating an infinite loop, unless there is a flow control command, such as break (which will exit the loop) or return .

+5
source

Its an endless cycle. Its equivalent:

 while (true) { } 

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

+4
source

Infinite loop

same as

 while(true){} 
+4
source

the code for(;;){} or while(true){} or while(1){} all represent infinite loops. An infinite loop is what can be expected in a software system that is expected to work at unlimited time. Each OS has at least one thing - this is how the background task or the inaction task is performed.

Real-time systems use infinite loops since the system must handle events that are asynchronous;

Basically, everything that runs the software uses infinite loops anyway.

+4
source

I don’t know why no one answered, why people do it instead of while(true) : This is because while(true) often generates a compilation warning that the loop condition is constant.

+2
source

This type of infinite loop can be used in microcontrollers. In your main function, you initialize the main functions of your microcontroller, then set while (1) .

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

The microprocessor will do nothing but wait for interrupts from internal or external devices (for example, timer overflows or UART data ready for reading) and respond to these interrupts.

0
source

Source: https://habr.com/ru/post/959348/


All Articles