{"id":"for-loops-bounds","slug":"bounds-from-input","title":"Bounds from input","text":"Both ends of a loop can come from the input. The loop then runs as many rounds as the numbers say:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int from = Integer.valueOf(scanner.nextLine());\n        int to = Integer.valueOf(scanner.nextLine());\n        for (int i = from; i <= to; i++) {\n            System.out.println(i);\n        }\n        System.out.println(\"End\");\n    }\n}\n```\n\n```input\n-2\n1\n```\n\n```output\n-2\n-1\n0\n1\nEnd\n```\n\nWhat if the start is bigger than the end, say from 5 to 3? The condition `5 <= 3` is false before the first round, so the loop's block doesn't run at all, and the program goes straight on to `End`. A loop that runs zero times is normal, and a program should still print something sensible when it happens.\n","task":"Read a start and an end, and print every number from the start to the end. For the input `3` and `6` the program prints:\n\n```\n3\n4\n5\n6\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        int end = 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        int end = Integer.valueOf(scanner.nextLine());\n        for (int i = start; i <= end; i++) {\n            System.out.println(i);\n        }\n    }\n}\n","hints":["The loop starts at `start` and runs while `i <= end`.","`for (int i = start; i <= end; i++)`"],"tests":[{"name":"Test 1","stdin":"3\n6\n","expect":"3\n4\n5\n6","hidden":false},{"name":"Test 2","stdin":"5\n5\n","expect":"5","hidden":true},{"name":"Test 3","stdin":"-2\n1\n","expect":"-2\n-1\n0\n1","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read a start and an end. Print every number from the start to the end, and then how many numbers that was, as `N numbers`. When the start is bigger than the end, only `0 numbers` is printed. For the input `4` and `6` the program prints:\n\n```\n4\n5\n6\n3 numbers\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        int end = 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        int end = Integer.valueOf(scanner.nextLine());\n        int count = 0;\n        for (int i = start; i <= end; i++) {\n            System.out.println(i);\n            count++;\n        }\n        System.out.println(count + \" numbers\");\n    }\n}\n","hints":["A counter before the loop, one more for each round.","When the start is bigger, the loop runs zero times and the counter stays 0: no special case is needed."],"tests":[{"name":"Test 1","stdin":"4\n6\n","expect":"4\n5\n6\n3 numbers","hidden":false},{"name":"Test 2","stdin":"9\n2\n","expect":"0 numbers","hidden":true},{"name":"Test 3","stdin":"-1\n1\n","expect":"-1\n0\n1\n3 numbers","hidden":true}],"require":[],"forbid":[]},{"task":"Read a start and an end, and print every second number from the start, as long as it's at most the end. For the input `1` and `9` the program prints:\n\n```\n1\n3\n5\n7\n9\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        int end = 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        int end = Integer.valueOf(scanner.nextLine());\n        for (int i = start; i <= end; i += 2) {\n            System.out.println(i);\n        }\n    }\n}\n","hints":["The change part of the `for` line can add more than one: `i += 2`.","`for (int i = start; i <= end; i += 2)`"],"tests":[{"name":"Test 1","stdin":"1\n9\n","expect":"1\n3\n5\n7\n9","hidden":false},{"name":"Test 2","stdin":"2\n7\n","expect":"2\n4\n6","hidden":true},{"name":"Test 3","stdin":"5\n5\n","expect":"5","hidden":true}],"require":[],"forbid":[]}]}