Encouraged
Any one of the following
while (1)
while (true)
for (;;)
are both good and explicit. They are all instantly recognizable by C and C++ programmers as infinite loops, and the compiler explicitly understands them as such.
Discouraged
Any good compiler should also recognize
while (1 < 2)
to be identical to the first or second above, since "1 < 2" is a constant
true expression. However, it
is a bit odd to the eyes of nearly all C and C++ programmers, and should be (IMO) avoided.
Using the GCC on WinXP/Pentium, all the above loops compiled to a single JMP instruction.
Flagged variations
However, a loop like:
1 2 3 4 5
|
bool done = false;
while (!done)
{
...
}
| |
does require code to check for continuation.
The power of and caveat to using (semi) infinite loops
Infinite loops are a wonderful control structure, because they give you
goto powers without encumbering any ire from others, via the
break and
continue statements.
One form of loop not mentioned is the run-once:
1 2 3 4 5
|
do
{
if (foo) break;
}
while (false);
| |
It is more or less an explicit example of the
goto workaround, and will be recognized by some as such.
It is often useful as a poor-man's (use in C only!) version of C++'s
try..
catch block.
Propriety
In all cases, however, I would suggest that the use of an infinite loop is likely an indication of design flaw. The number of times that it is "correct" to use one is far fewer than given in practice...
My $0.02.
Hope this helps.