{"id":"for-loops-for","slug":"for-loops","title":"for loops","text":"Counting loops are so common that Java has a shorter way to write them. The **for loop** puts all three parts on one line, separated by semicolons:\n\n```java\nfor (int i = 1; i <= 5; i++) {\n    System.out.println(i);\n}\n```\n\n```output\n1\n2\n3\n4\n5\n```\n\n`for (start; condition; change)` does the same as this `while` loop:\n\n```java\nint i = 1;\nwhile (i <= 5) {\n    System.out.println(i);\n    i++;\n}\n```\n\n- The start runs once, before the first round.\n- The condition is checked before every round.\n- The change runs at the end of every round.\n\nA loop variable is often called `i`. Made in the `for` line, it only exists inside the loop. Counting down and other steps work too: `for (int i = 10; i >= 1; i--)` counts from 10 to 1, and `i += 2` instead of `i++` takes every second number.\n","task":"Rewrite this `while` loop as a `for` loop. The program prints:\n\n```\nLap 1\nLap 2\nLap 3\nLap 4\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        int lap = 1;\n        while (lap <= 4) {\n            System.out.println(\"Lap \" + lap);\n            lap++;\n        }\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        for (int lap = 1; lap <= 4; lap++) {\n            System.out.println(\"Lap \" + lap);\n        }\n    }\n}\n","hints":["The three parts are already there: `int lap = 1`, `lap <= 4` and `lap++`.","`for (int lap = 1; lap <= 4; lap++) { ... }`, and the loop's block keeps only the print line."],"tests":[{"name":"Output","stdin":"","expect":"Lap 1\nLap 2\nLap 3\nLap 4","hidden":false}],"require":[{"pattern":"\\bfor\\s*\\(","message":"Use a `for` loop."}],"forbid":[{"pattern":"\\bwhile\\b","message":"Replace the `while` loop with the `for` loop."}],"more":[{"task":"Read a start number and, with a `for` loop, print every whole number from it up to 10. For the input `7` the program prints:\n\n```\n7\n8\n9\n10\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int start = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int start = Integer.valueOf(scanner.nextLine());\n        for (int i = start; i <= 10; i++) {\n            System.out.println(i);\n        }\n    }\n}\n","hints":["The loop variable starts from the number that was read.","`for (int i = start; i <= 10; i++)`"],"tests":[{"name":"Test 1","stdin":"7\n","expect":"7\n8\n9\n10","hidden":false},{"name":"Test 2","stdin":"10\n","expect":"10","hidden":true},{"name":"Test 3","stdin":"1\n","expect":"1\n2\n3\n4\n5\n6\n7\n8\n9\n10","hidden":true}],"require":[{"pattern":"\\bfor\\s*\\(","message":"Use a `for` loop."}],"forbid":[]},{"task":"Read a number and, with a `for` loop, print its times table from 1 to 10, in the form `1 x 7 = 7`. For the input `7` the program prints:\n\n```\n1 x 7 = 7\n2 x 7 = 14\n3 x 7 = 21\n4 x 7 = 28\n5 x 7 = 35\n6 x 7 = 42\n7 x 7 = 49\n8 x 7 = 56\n9 x 7 = 63\n10 x 7 = 70\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n        for (int i = 1; i <= 10; i++) {\n            System.out.println(i + \" x \" + number + \" = \" + (i * number));\n        }\n    }\n}\n","hints":["The loop counts from 1 to 10, and each round prints one line of the table.","Put the multiplication in parentheses: `(i * number)`."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"1 x 7 = 7\n2 x 7 = 14\n3 x 7 = 21\n4 x 7 = 28\n5 x 7 = 35\n6 x 7 = 42\n7 x 7 = 49\n8 x 7 = 56\n9 x 7 = 63\n10 x 7 = 70","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"1 x 1 = 1\n2 x 1 = 2\n3 x 1 = 3\n4 x 1 = 4\n5 x 1 = 5\n6 x 1 = 6\n7 x 1 = 7\n8 x 1 = 8\n9 x 1 = 9\n10 x 1 = 10","hidden":true},{"name":"Test 3","stdin":"12\n","expect":"1 x 12 = 12\n2 x 12 = 24\n3 x 12 = 36\n4 x 12 = 48\n5 x 12 = 60\n6 x 12 = 72\n7 x 12 = 84\n8 x 12 = 96\n9 x 12 = 108\n10 x 12 = 120","hidden":true}],"require":[{"pattern":"\\bfor\\s*\\(","message":"Use a `for` loop."}],"forbid":[]}]}