Java for 循环继续

标签 java for-loop continue

if i++ = 11,这将结束 for 循环,但我很困惑为什么 i = 11 将被存储并在下一个 println 中打印出来

public class kkk {
    public static void main(String args[]) {
        int i;
        for (i = 1; i < 11; i++) {
            if (i > 5)
                continue;
            System.out.println(i);
        }
        System.out.println(i);
    }
}

最佳答案

为了解释 for 循环的逻辑,我们将其与 while 进行比较

for (
    i = 1;   //Block of initialisation
    i < 11;  //condition
    i++      //Block of incrementation

){
    if (i > 5)
        continue;
    System.out.println(i);
}

这看起来像

{ //Block of initialisation
    i = 1
}
while(i < 11){ //condition
    { //Block of code
        if (i > 5)
            continue;
        System.out.println(i);

    }
    { //Block of incrementation
        ++i;
    }
}

在这里您将看到 for 循环每个 block 的确切顺序。因此,退出循环时,i 确实等于 11

注意: block 语句并不真正存在,它用于显示所有内容都存在的位置。当然,初始化中声明的变量不会存在于 block 之外

关于Java for 循环继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44063112/

相关文章:

Java-执行流程

javascript - 如何将在 for 循环中选中的复选框中的数组值推送到另一个数组?

Java FOR 循环递增并将新变量传递给方法

PHP - else, 'escape' 嵌套并跳到elseif

Java.Lang.Exception 与 Math.pow

java - 我应该在哪里初始化我的开关小部件 Android

java - 如何从 JBoss 中打开的连接池正确保持数据库连接

java - 嵌套 "FOR-loops"不起作用

javascript - Ractive.js 继续和中断循环

python - else 在继续、中断或返回之后