35

Is it this kind of thing:

for(;;)
 {
   statements;
 }

Or is it this:

for(initialisation;condition;updation)
{
}

I am looking for answers with references to a variety of sources.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
vidit jain
  • 493
  • 1
  • 5
  • 13
  • Read also: [Is an (empty) infinite loop undefined behavior in C?](http://stackoverflow.com/questions/15595493/is-an-empty-infinite-loop-undefined-behavior-in-c) – Deduplicator Jul 01 '14 at 12:00

6 Answers6

46

Your first case (for with empty expressions) is an infinite loop and the second one (with empty body of the for statement) is an empty loop

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
6

In my environment it is like this:

for(;;) { statements; }

endless loop

for(initialisation;condition;updation) { } 

empty loop

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
2

Answer is context dependent.

If you mean an empty for loop, then

 for(;;)
 {
     statements;
 }

is such a thing.

Although, the same thing can be achieved with a while loop:

while(true)
{
    statements;
}

and this isn't an "empty" loop. Both of these are infinite loops that you must break out of using break inside of your loop.

On the other hand,

for(initialisation;condition;updation)
{
}

this is an "empty" loop that bascially does nothing, except perhaps update some variables that could be defined before the loop itself.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • 1
    The last for-loop could very well do lots and lots of things, because the updatestatement can have all sorts of side effects - if you wanted to you could stuff most of your application in it. I certainly advise against it from maintainability point of view, but assuming that the last 'basically does nothing' when you find such a loop in unknown code certainly isn't wise. – Inca Jan 04 '11 at 10:25
  • 1
    I think you need to edit, you're claiming that the first `for` loop is both empty and not empty. – unwind Jan 04 '13 at 10:10
  • 1
    Example of a place an empty loop could do something: `while( ((c=getchar()) != '\n') && (c != EOF)) {}` (where `c` is an `int`). Seeks through `stdin` to the first found newline or end of stream. – Wyatt Ward Jan 14 '22 at 19:43
2

An empty loop is a loop which has an empty body, e.g.

for(int i = 0; i < 10; ++i) {}
while(cin) {}

(note that the second example here also happens to be endless)

There are cases where these are useful, for example when a function has a desired side-effect and returns its success, and should repeated until unsuccessful, for example to read the last line in a file:

std::string getLastLine(std::string filename)
{
  std::ifstream in(filename.c_str());
  if(!in)
    return "";

  std::string line;
  while(std::getline(in, line)); // empty loop, the operation returns the condition
  return line;
}
Mephane
  • 1,984
  • 11
  • 18
1

It equals to that:

while (true) {
  statements;
}

Infinite for loop is a loop that works until something else stops it.

Samet Atdag
  • 982
  • 6
  • 21
1
for(;;)
 {
   statements;
 }

is endless loop because there is redundant value/Grabage value which makes the loop true

for(initialisation;condition;updation)
 {
   body;
 }

is just syntax for for loop (educational purpose use)

IlGala
  • 3,331
  • 4
  • 35
  • 49
VickyP
  • 21
  • 4