简体   繁体   English

为什么“for(;;)”的测试条件成功了?

[英]Why Does Test Condition of “for(;;)” Succeed?

Insomuch as I understand "for(;;)" has no initial condition, no test condition and no increment condition, and therefore loops forever, I am curious why the test condition succeeds each loop. 因为我理解“for(;;)”没有初始条件,没有测试条件和没有增量条件,因此永远循环,我很好奇为什么测试条件成功每个循环。

Does the empty expression ";" 空表达式是否为“;” evaluate as true in C? 在C中评估为真? Or is this a special case handled by compilers? 或者这是由编译器处理的特殊情况?

A similar, but unrelated question . 一个类似但无关的问题

This is by the definition of the for statement in the C language. 这是通过C语言中for语句的定义。 6.8.5.3/2 "The for statement": 6.8.5.3/2“的for声明”:

Both clause-1 and expression-3 can be omitted. 可以省略子句-1和表达式3。 An omitted expression-2 is replaced by a nonzero constant . 省略的表达式-2由非零常量替换

C language has no such thing as "empty expression". C语言没有“空表达”这样的东西。 If any expression is missing from the for statement, syntactically it means that the entire expression is omitted , not that it is there, but empty. 如果for语句中缺少任何表达式,从语法上讲,它意味着整个表达式被省略 ,而不是它在那里,而是空的。

A for loop with an omitted second expression loops forever because the specification of for statement says so. 一个for与省略第二表达式循环,循环永远,因为规范for声明是这样说的。 Ie it is a dedicated feature specific to for alone, not something more generic. 即它是专门for单独的专用功能,而不是更通用的功能。

Additionaly (a terminological nitpick) only the second expression is really a condition . 另外(一个术语的挑剔)只有第二个表达才是真正的条件 The first and the third are not really "conditions". 第一个和第三个不是真正的“条件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM