{"id":"loops-continue","slug":"continue","title":"continue","text":"`break` leaves the loop. Its partner `continue` only skips the rest of this round: the loop goes straight back to its start and carries on. It's handy for skipping input that shouldn't be handled:\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        while (true) {\n            int number = Integer.valueOf(scanner.nextLine());\n            if (number == 0) {\n                break;\n            }\n            if (number % 2 != 0) {\n                System.out.println(number + \" is odd, skipped\");\n                continue;\n            }\n            System.out.println(\"Half of \" + number + \" is \" + number / 2);\n        }\n    }\n}\n```\n\n```input\n8\n5\n12\n0\n```\n\n```output\nHalf of 8 is 4\n5 is odd, skipped\nHalf of 12 is 6\n```\n\nFor 5 the `continue` runs, so the \"Half of\" line is skipped for that round only. The loop then reads 12 as usual.\n\nA loop often starts with its checks, each ending in `break` or `continue`, and then does the real work for the input that got through them.\n","task":"Read test scores until `-1`. A score above 100 is a typing mistake: print `Invalid score` and skip it with `continue`. Print every other score as `Score: X`. For the input `85`, `120`, `70` and `-1` the program prints:\n\n```\nScore: 85\nInvalid score\nScore: 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\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        while (true) {\n            int score = Integer.valueOf(scanner.nextLine());\n            if (score == -1) {\n                break;\n            }\n            if (score > 100) {\n                System.out.println(\"Invalid score\");\n                continue;\n            }\n            System.out.println(\"Score: \" + score);\n        }\n    }\n}\n","hints":["First check for `-1` and `break`. Then check for a score over 100.","Inside the second `if`: print `Invalid score`, then `continue;`"],"tests":[{"name":"Test 1","stdin":"85\n120\n70\n-1\n","expect":"Score: 85\nInvalid score\nScore: 70","hidden":false},{"name":"Test 2","stdin":"101\n-1\n","expect":"Invalid score","hidden":true},{"name":"Test 3","stdin":"100\n101\n0\n-1\n","expect":"Score: 100\nInvalid score\nScore: 0","hidden":true}],"require":[{"pattern":"\\bcontinue\\s*;","message":"Skip invalid scores with `continue`."}],"forbid":[],"more":[{"task":"Read numbers until 0. Skip negative numbers with `continue`, without printing anything, and print the square of every positive one. For the input `3`, `-2`, `4` and `0` the program prints:\n\n```\n9\n16\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\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        while (true) {\n            int number = Integer.valueOf(scanner.nextLine());\n            if (number == 0) {\n                break;\n            }\n            if (number < 0) {\n                continue;\n            }\n            System.out.println(number * number);\n        }\n    }\n}\n","hints":["The negative case only needs `continue;`: nothing is printed.","The square is the number times itself."],"tests":[{"name":"Test 1","stdin":"3\n-2\n4\n0\n","expect":"9\n16","hidden":false},{"name":"Test 2","stdin":"-5\n-1\n7\n0\n","expect":"49","hidden":true},{"name":"Test 3","stdin":"10\n0\n","expect":"100","hidden":true}],"require":[{"pattern":"\\bcontinue\\s*;","message":"Skip negative numbers with `continue`."}],"forbid":[]},{"task":"Read ages until 0. An age below 1 or above 120 isn't a real age: print `Not a real age` and skip it. For the others, print how old the person will be in 10 years. For the input `25`, `150`, `7` and `0` the program prints:\n\n```\nIn 10 years: 35\nNot a real age\nIn 10 years: 17\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\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        while (true) {\n            int age = Integer.valueOf(scanner.nextLine());\n            if (age == 0) {\n                break;\n            }\n            if (age < 1 || age > 120) {\n                System.out.println(\"Not a real age\");\n                continue;\n            }\n            System.out.println(\"In 10 years: \" + (age + 10));\n        }\n    }\n}\n","hints":["Check for 0 first, so it stops the loop instead of counting as \"not a real age\".","Put `age + 10` in parentheses inside the print line, so it's added before it's joined to the text."],"tests":[{"name":"Test 1","stdin":"25\n150\n7\n0\n","expect":"In 10 years: 35\nNot a real age\nIn 10 years: 17","hidden":false},{"name":"Test 2","stdin":"-3\n120\n1\n0\n","expect":"Not a real age\nIn 10 years: 130\nIn 10 years: 11","hidden":true},{"name":"Test 3","stdin":"121\n0\n","expect":"Not a real age","hidden":true}],"require":[{"pattern":"\\bcontinue\\s*;","message":"Skip the ages that aren't real with `continue`."}],"forbid":[]}]}