gpt4 book ai didi

java for循环以 "true"作为停止条件?

转载 作者:行者123 更新时间:2023-12-01 06:02:52 26 4
gpt4 key购买 nike

我需要实现一个程序,其中包含以下代码:

for (int n = 1024; true; n+=n)

我找不到具有这种格式的 java 循环的任何其他示例。这是什么意思?我尝试过研究它,但我什至不知道要搜索什么 - 这对我来说完全陌生。

最佳答案

基本的 for 语句 is described in the language spec :

for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

您询问的是Expressiontrue时的情况。 (上面的方括号表示它是可选的)。

其含义如下所述,在 Sec 14.14.1.2 中。 :

  • If the Expression is not present, or it is present and the value resulting from its evaluation (including any possible unboxing) is true, then the contained Statement is executed. 

...

  • If the Expression is present and the value resulting from its evaluation (including any possible unboxing) is false, no further action is taken and the forstatement completes normally.

因此,Expression 存在,并且计算结果为 true(因为 true 计算结果为 true)。因此,Statement 被执行,并且将继续执行,因为 Expression 保持 true

因此,它是一个无限循环(除非循环内有中断、返回、抛出或System.exit)。

关于java for循环以 "true"作为停止条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53641828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com