gpt4 book ai didi

java - For循环自动递增变量

转载 作者:行者123 更新时间:2023-11-30 01:45:05 25 4
gpt4 key购买 nike

for(int i=0; i < 5; i++)循环不是 i增加 i++在 printf 读取它之前就已经了吗?如果是这样,它应该返回 i=1正确的?这里返回0是什么概念?一开始。

public class Application {
public static void main(String[] args) {

for(int i=0; i < 5; i++) {
System.out.printf("The value of i is: %d\n", i);
}
}
}

最佳答案

14.14.1. The basic for Statement

BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
  • 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. Then there is a choice:

    • If execution of the Statement completes normally, then the following two steps are performed in sequence:

      • First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If evaluation of any expression completes abruptly for some reason, the for statement completes abruptly for the same reason; any ForUpdate statement expressions to the right of the one that completed abruptly are not evaluated.

      • Second, another for iteration step is performed.

在您的示例中,这意味着 i++ 将在 System.out.printf 行之后执行。

关于java - For循环自动递增变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363154/

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