为什么 java for 循环的空条件语句的计算结果为真?
why does empty conditional statement for java for loop evaluate to true?
如果有人在 Empty for loop - for(;;) 或网站上的其他地方问过这个问题,我深表歉意。
我想知道是否有人知道为什么 java 会选择 parser/compiler 将 for(;;){} 循环中的空条件语句评估为 true 而不是 null 或空白 ?
由用户@Mchi 链接
https://developer.mozilla.org/en/JavaScript/Reference/Statements/for
"An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct."
我只是好奇
条件语句必须是 boolean
类型才能控制循环,与 if()
或 while()
语句中的表达式必须是 boolean
类型。
boolean
的唯一有效值是 true
和 false
。所以,null
和 void
不适用,它们没有任何意义。
至于空条件语句,在 true
和 false
这两个可能值中,他们选择了 true
因为绝对不需要一个不会循环的循环所有,虽然在很多情况下我们需要一个将永远循环的循环(或直到 break
.)
如果有人在 Empty for loop - for(;;) 或网站上的其他地方问过这个问题,我深表歉意。
我想知道是否有人知道为什么 java 会选择 parser/compiler 将 for(;;){} 循环中的空条件语句评估为 true 而不是 null 或空白 ?
由用户@Mchi 链接
https://developer.mozilla.org/en/JavaScript/Reference/Statements/for
"An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct."
我只是好奇
条件语句必须是 boolean
类型才能控制循环,与 if()
或 while()
语句中的表达式必须是 boolean
类型。
boolean
的唯一有效值是 true
和 false
。所以,null
和 void
不适用,它们没有任何意义。
至于空条件语句,在 true
和 false
这两个可能值中,他们选择了 true
因为绝对不需要一个不会循环的循环所有,虽然在很多情况下我们需要一个将永远循环的循环(或直到 break
.)