{"id":"loops-while-break","slug":"while-and-break","title":"while (true) and break","text":"A **loop** runs the same lines again and again. The simplest loop in Java is `while (true)`: the block after it repeats forever, until a `break` inside it ends the loop.\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            System.out.println(\"Type a word (end stops):\");\n            String word = scanner.nextLine();\n            if (word.equals(\"end\")) {\n                break;\n            }\n            System.out.println(\"You typed \" + word);\n        }\n        System.out.println(\"Bye!\");\n    }\n}\n```\n\n```input\ncat\ndog\nend\n```\n\n```output\nType a word (end stops):\nYou typed cat\nType a word (end stops):\nYou typed dog\nType a word (end stops):\nBye!\n```\n\nEach time round, the loop asks, reads a word and checks it. For `cat` and `dog` it prints the word and starts again from the top. For `end`, `break` jumps straight out of the loop, to the first line after its closing `}`.\n\nA `while (true)` loop needs a way out. Here the input is the way out: if it never contains `end`, the loop keeps reading until the input runs out, and the program crashes with a `NoSuchElementException` (\"No line found\"): it asked for more input than it was given. A loop that doesn't read anything and has no `break` would never stop at all; on this site such a program is stopped after 10 seconds, with a note that it probably has an endless loop.\n","task":"Fill in the blanks, so the loop keeps reading until the password is `open sesame`. For the input `abracadabra` and then `open sesame` the program prints:\n\n```\nThe door stays shut.\nThe door opens!\n```\n","kind":"fill","seed":"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            String attempt = scanner.nextLine();\n            if (attempt.equals(\"open sesame\")) {\n                [[break]];\n            }\n            System.out.println(\"The door stays shut.\");\n        }\n        System.out.println(\"The door opens!\");\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            String attempt = scanner.nextLine();\n            if (attempt.equals(\"open sesame\")) {\n                break;\n            }\n            System.out.println(\"The door stays shut.\");\n        }\n        System.out.println(\"The door opens!\");\n    }\n}\n","hints":["The loop repeats forever until something stops it, so its condition is always true.","The word that leaves a loop is `break`."],"tests":[{"name":"Test 1","stdin":"abracadabra\nopen sesame\n","expect":"The door stays shut.\nThe door opens!","hidden":false},{"name":"Test 2","stdin":"open sesame\n","expect":"The door opens!","hidden":true},{"name":"Test 3","stdin":"please\nsesame\nOpen Sesame\nopen sesame\n","expect":"The door stays shut.\nThe door stays shut.\nThe door stays shut.\nThe door opens!","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A lift reads floor numbers. For each one it prints `Going to floor X`, until the number is 0: then it prints `Back on the ground floor` and stops. Use a `while (true)` loop and leave it with `break`. For the input `3`, `5` and `0` the program prints:\n\n```\nGoing to floor 3\nGoing to floor 5\nBack on the ground floor\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 floor = Integer.valueOf(scanner.nextLine());\n            if (floor == 0) {\n                break;\n            }\n            System.out.println(\"Going to floor \" + floor);\n        }\n        System.out.println(\"Back on the ground floor\");\n    }\n}\n","hints":["Read the number inside the loop, so each round reads a new one.","Check for 0 right after reading and `break`; the last line goes after the loop."],"tests":[{"name":"Test 1","stdin":"3\n5\n0\n","expect":"Going to floor 3\nGoing to floor 5\nBack on the ground floor","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Back on the ground floor","hidden":true},{"name":"Test 3","stdin":"12\n-1\n2\n0\n","expect":"Going to floor 12\nGoing to floor -1\nGoing to floor 2\nBack on the ground floor","hidden":true}],"require":[{"pattern":"while\\s*\\(\\s*true\\s*\\)","message":"Use a `while (true)` loop."},{"pattern":"\\bbreak\\s*;","message":"Leave the loop with `break`."}],"forbid":[]},{"task":"A weather station reads temperatures. A reading below -50 or above 60 means the sensor is broken. Print each normal reading as `Reading: X`; at the first broken one, print `Sensor error` and leave the loop with `break`. For the input `12`, `15` and `99` the program prints:\n\n```\nReading: 12\nReading: 15\nSensor error\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 reading = Integer.valueOf(scanner.nextLine());\n            if (reading < -50 || reading > 60) {\n                System.out.println(\"Sensor error\");\n                break;\n            }\n            System.out.println(\"Reading: \" + reading);\n        }\n    }\n}\n","hints":["A broken reading is `reading < -50 || reading > 60`.","In that case print `Sensor error` and then `break`, both inside the `if`."],"tests":[{"name":"Test 1","stdin":"12\n15\n99\n","expect":"Reading: 12\nReading: 15\nSensor error","hidden":false},{"name":"Test 2","stdin":"-60\n","expect":"Sensor error","hidden":true},{"name":"Test 3","stdin":"-50\n60\n0\n61\n","expect":"Reading: -50\nReading: 60\nReading: 0\nSensor error","hidden":true}],"require":[{"pattern":"\\bbreak\\s*;","message":"Leave the loop with `break`."}],"forbid":[]}]}