Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on for loop.

Status
Not open for further replies.

coffytan

Programmer
Mar 25, 2004
12
MY
Dear,
Recently I found an example program from the net. There's 1 line of statement it specified like this: for(;;).
When I compile, the compilation is successful. Anybody can tell me what are this for loop doing??
Thank you in advance.


Best regards,

Coffy Tan
 
There is no initialization statement, therefore nothing happens in the initialization step before starting the loop.

There is no stop condition, so the loop continues until a break or return is encountered, or an exception is thrown (or a longjmp, or a goto outside the loop, or... ).

There is no update statement, so nothing is done between each iteration of the loop.


A faster and easier way to get an answer to this question would be to try it and find out yourself.
 
Some addition (syntax & pragmatics;):
any clause of for(prelude;condition;interlude) may be empty.
This 'degenerated' form may be useful in a program case like:
Code:
for (;;)
{
  do non-trivial calculations (or some test case);
  make decision if you may (must) continue or must break;
  do some works (with previous result or what else);
  // and try again
}
The 2nd program case: main loop of non-stop service (until a server die)...
 
It is the efficient alternative to
Code:
while (1)
{
...
}

or

do {
...
} while (1);
Why efficient? No comparison is done. You can always compare the code generation. Some optimizing compilers will generate the same code on all three.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top