-4

Sometimes when im reading code it appears to be written for with no params to load

Like this:

for(;;)
{
/*Some piece of code*/
}
Albvalort
  • 5
  • 1

2 Answers2

2

Any combination of the three expressions can omitted, including all of them.

If the first or third expression is omitted, no code is evaluated when those expression would be evaluated. (If both of these are omitted, one effectively has a pure while loop.)

If the middle expression is omitted, it's as if a true expression was provided. This creates a loop that can only be exited using break, return, exit and long jumps.

For example, the following loops until a valid answer is given.

int x;
for (;;) {              // Or `while (1)`
   display_prompt();
   x = get_answer();
   if (is_valid(x))
      break;

   printf("Invalid input\n");
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
1

The for loop has three parts: initialization, test, and update.

Any of these can be omitted. By having none of them included, you've created an infinite loop and need to include some kind of test with break inside the body of the loop.

For the same effect, one can use a while loop:

while (1) {

}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • Omitting only the middle of the three sections of a for loop ( `for(int i = 0;;i++)` ) is sufficient to make it infinite. – ryyker Oct 25 '21 at 14:44
  • Or by omitting the update, assuming nothing is updated in the body and the rest doesn't have a side effect which performs the update. – Chris Oct 25 '21 at 14:46