Why is for (;;) {...} an infinite loop?

Possible duplicate:
Empty for loop - for (;;)

I just found a strange design in the JS-syntax UglifyJS (in L1045 ): for(;;){…}

.

I assumed that an empty condition would resolve to undefined

, which would convert to boolean false

. But this is definitely not the case.

Apparently it starts an endless loop. I was able to reproduce this behavior, but I don't know why. Any (logical) explanations?

Also: when possible, why doesn't it work while(){…}

?

+3


source to share


4 answers


This is just a definition of semantics. The missing "test" expression is treated as a value expression true

. Languages ​​are composed by humans, and they can indicate any behavior they like. Clearly this kind of behavior is what Mr. Eich likes :-)



+4


source


for(;;){…}

interprets empty as a condition true

and while(){}

is not considered valid. As stated above, it is completely language dependent, but described in the specification.



+4


source


The ECMA-262 specification language JavaScript (Section 12.6.3) is determined, as it should look for loop behavior.

It can be seen from the definition that if the information around the semi-columns is not available, there are no conditions to exit the cycle. The only way to break out of the loop is to define a test condition and possibly some start and step values.

Behavior can be defined differently, but it is not.

+1


source


From spec .

12.6.3 The for Statement
    The production
        IterationStatement : for (ExpressionNoIn(opt) ; Expression(opt) ; Expression(opt)) Statement
    is evaluated as follows:
    1. If ExpressionNoIn is present, then.
        a. Let exprRef be the result of evaluating ExpressionNoIn.
        b. Call GetValue(exprRef). (This value is not used but the call may have side-effects.)
    2. Let V = empty.
    3. Repeat
        a. If the first Expression is present, then
            i. Let testExprRef be the result of evaluating the first Expression.
            ii. If ToBoolean(GetValue(testExprRef)) is false, return (normal, V, empty) .
        b. Let stmt be the result of evaluating Statement.Β© Ecma International 2011 91
        c. If stmt.value is not empty, let V = stmt.value
        d. If stmt.type is break and stmt.target is in the current label set, return (normal, V, empty) .
        e. If stmt.type is not continue || stmt.target is not in the current label set, then
            i. If stmt is an abrupt completion, return stmt.
        f. If the second Expression is present, then
            i. Let incExprRef be the result of evaluating the second Expression.
            ii. Call GetValue(incExprRef). (This value is not used.

      

The gist of this specification is : for a statement, it stops when the first expression returns "falsey".

Since the absence of an expression does not return false, the script will run forever (or until the statement break

is executed from within the loop body).

+1


source







All Articles