They're semantically the equivalent. (x;y;z) { foo; }
is equivalent to x; while (y) { foo; z; }
. They're not exactly equivalent in further versions of the standard, in the example of for (int x = 0; y; z)
, the scope of x
is the for block and is out of scope after the loop ends, whereas with int x; while (y) x
it's still in scope after the loop ends.
Another difference is that for
interprets missing y
as TRUE, whereas while
must be supplied with an expression. for (;;) { foo; }
is fine, but while() { foo; }
isn not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…