{"id":"for-loops-condition-checked","slug":"when-the-condition-is-checked","title":"When the condition is checked","text":"A loop's condition is checked at one moment only: **before each round**, including the very first. It isn't watched during the round. If the variable passes the limit halfway through the block, the rest of the block still runs, and the loop stops only at the next check.\n\n```java\nint total = 0;\nint rounds = 0;\nwhile (total < 10) {\n    total += 4;\n    rounds++;\n    System.out.println(\"Round \" + rounds + \": total \" + total);\n}\nSystem.out.println(\"After the loop: \" + total);\n```\n\n```output\nRound 1: total 4\nRound 2: total 8\nRound 3: total 12\nAfter the loop: 12\n```\n\nIn round 3, `total` went past 10, but the round finished and printed. Only then did the check `12 < 10` fail. So after a loop, its variable holds the first value that made the condition false, which isn't necessarily the limit.\n\nTracing a loop by hand (writing down each variable after each round) is the surest way to know what it does. These three challenges are for tracing.\n","task":"What does this program print? Trace `i` and `rounds` round by round.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int i = 0;\n        int rounds = 0;\n        while (i < 10) {\n            i += 3;\n            rounds++;\n        }\n        System.out.println(\"Rounds: \" + rounds);\n        System.out.println(\"i: \" + i);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int i = 0;\n        int rounds = 0;\n        while (i < 10) {\n            i += 3;\n            rounds++;\n        }\n        System.out.println(\"Rounds: \" + rounds);\n        System.out.println(\"i: \" + i);\n    }\n}\n","hints":["`i` goes 0, 3, 6, 9, and then one more round, because 9 is still below 10.","The fourth round makes `i` 12 and `rounds` 4. Then `12 < 10` is false."],"tests":[{"name":"Output","stdin":"","expect":"Rounds: 4\ni: 12","hidden":false}],"require":[],"forbid":[],"lines":["Rounds: 4","i: 12"],"more":[{"task":"What does this program print? Two variables change in each round.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int a = 1;\n        int b = 10;\n        while (a < b) {\n            a *= 2;\n            b--;\n        }\n        System.out.println(a);\n        System.out.println(b);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int a = 1;\n        int b = 10;\n        while (a < b) {\n            a *= 2;\n            b--;\n        }\n        System.out.println(a);\n        System.out.println(b);\n    }\n}\n","hints":["After each round: a 2 and b 9, then a 4 and b 8, then a 8 and b 7.","Now `8 < 7` is false, so the loop ends with a 8 and b 7."],"tests":[{"name":"Output","stdin":"","expect":"8\n7","hidden":false}],"require":[],"forbid":[],"lines":["8","7"]},{"task":"What does this program print? The loop's variable also changes inside the block.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        for (int i = 0; i < 6; i++) {\n            System.out.println(i);\n            i++;\n        }\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        for (int i = 0; i < 6; i++) {\n            System.out.println(i);\n            i++;\n        }\n    }\n}\n","hints":["Each round adds one inside the block, and the `for` line adds another one at the end of the round.","So `i` is 0, then 2, then 4 when it's printed. After that it becomes 6, and `6 < 6` is false."],"tests":[{"name":"Output","stdin":"","expect":"0\n2\n4","hidden":false}],"require":[],"forbid":[],"lines":["0","2","4"]}]}