Page 1 of 1

Infinite loop

Posted: Wed Sep 12, 2007 2:29 pm
by JonM
What is the preferred implementation of an infinite loop?

Code: Select all

for ( ; ; ) ...
or

Code: Select all

while (1 == 1) ...
or

something else?

Thanks in advance.

Jonathan

Posted: Wed Sep 26, 2007 3:19 pm
by misra-c
In the TC, we allow the for loop example with all three expressions being empty for a deliberate infinite loop.

Code: Select all

/* deliberate infinite loop */
for ( ; ; )
{
   ...
}

Posted: Thu Sep 27, 2007 3:00 pm
by JonM
Yes I saw that it was allowed. Is while (1==1) also allowed? Which is preferred?

Posted: Wed Jan 09, 2008 4:11 pm
by George Brown
Hi Jon,

Looks like \"they\" said

Code: Select all

/* deliberate infinite loop */ 
for ( ; ; ) 
{ 
   ... 
}
was the allowed form is infinite loops. Given one way to write them, there is no need for alternatives.

I was taught Pascal back in the early 80's, and would like to write this in the following form. C99 defines a \"boolean\" type, so MISRA-C3 could be different!

Code: Select all

/* deliberate infinite loop */ 
while(true) 
{ 
   ... 
}
George