Why for (;;); endless cycle?

I wrote endless loops like these many times over the years in C and C ++, but today I first thought about it - why is it an endless loop when the condition condition is empty? One would expect that you need to write something like this for(;true;);to get a valid infinite loop?

while(); does not compile and does not execute while(;);

In any case, I like the syntax for(;;);and use it often, but is it a special case to consider an empty conditional block as true, or are there other cases in C or C ++ where the expression for an empty condition is interpreted as true?

+6
source share
2 answers

C for:

C11 ยง6.8.5.3 2

-1 -3 . -2 .

, ++:

++ 14 ยง6.5.3 2

. while ().

+11

(++). :

:

attr(optional) for ( init-statement condition(optional) ; iteration_expression(optional) ) statement

( )

, :

{
    init_statement 
    while ( condition ) { 
        statement 
        iteration_expression ; 
    }
}

,

3) while(true)

while , , .

attr(optional) while ( condition ) statement
+3

Source: https://habr.com/ru/post/1015390/


All Articles