{"id":"for-loops-while-condition","slug":"loop-conditions","title":"while with a condition","text":"`while (true)` needs a `break` to stop. A loop can instead have a real **condition**: it's checked before every round, and the loop ends the first time that check finds it false.\n\n```java\nint number = 1;\nwhile (number <= 5) {\n    System.out.println(number);\n    number++;\n}\nSystem.out.println(\"Done, number is now \" + number);\n```\n\n```output\n1\n2\n3\n4\n5\nDone, number is now 6\n```\n\nThis is a **counting loop**, and it has three parts:\n\n1. a variable set before the loop (`int number = 1;`),\n2. a condition that decides whether another round runs (`number <= 5`),\n3. a change inside the loop that moves toward the end (`number++`).\n\nForget the third part and `number` stays 1 forever: the condition never turns false.\n\nWhen the loop should end at a known point, a condition says so right at the top. `while (true)` with `break` suits loops that end on something the loop reads, like a word such as `end`.\n","task":"Fill in the condition, so the loop prints 10, 20, 30, 40 and 50, one per line.\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        int number = 10;\n        while ([[number <= 50‖number < 60‖number < 51‖number != 60]]) {\n            System.out.println(number);\n            number += 10;\n        }\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int number = 10;\n        while (number <= 50) {\n            System.out.println(number);\n            number += 10;\n        }\n    }\n}\n","hints":["The loop should still run when `number` is 50, and stop when it reaches 60.","`number <= 50`"],"tests":[{"name":"Output","stdin":"","expect":"10\n20\n30\n40\n50","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Read a number of steps and print `Step 1`, `Step 2` and so on, up to that number. Use `while` with a condition, not `while (true)`. For the input `3` the program prints:\n\n```\nStep 1\nStep 2\nStep 3\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 steps = 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 steps = Integer.valueOf(scanner.nextLine());\n        int step = 1;\n        while (step <= steps) {\n            System.out.println(\"Step \" + step);\n            step++;\n        }\n    }\n}\n","hints":["A counter from 1: `int step = 1;` before the loop.","`while (step <= steps)`, and `step++;` at the end of the loop's block."],"tests":[{"name":"Test 1","stdin":"3\n","expect":"Step 1\nStep 2\nStep 3","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"Step 1","hidden":true},{"name":"Test 3","stdin":"6\n","expect":"Step 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6","hidden":true}],"require":[{"pattern":"while\\s*\\(","message":"Use a `while` loop."}],"forbid":[{"pattern":"while\\s*\\(\\s*true\\s*\\)","message":"Give the loop a real condition instead of `true`."}]},{"task":"Read a whole number, then keep halving it (whole-number division by 2) and print each value while it's above 0. Use a condition, not `break`. For the input `20` the program prints:\n\n```\n20\n10\n5\n2\n1\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        while (number > 0) {\n            System.out.println(number);\n            number /= 2;\n        }\n    }\n}\n","hints":["The loop runs while `number > 0`.","Print first, then halve: `number /= 2;` (5 halved is 2, because the decimals are dropped)."],"tests":[{"name":"Test 1","stdin":"20\n","expect":"20\n10\n5\n2\n1","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"1","hidden":true},{"name":"Test 3","stdin":"100\n","expect":"100\n50\n25\n12\n6\n3\n1","hidden":true}],"require":[],"forbid":[{"pattern":"\\bbreak\\b","message":"Let the loop's condition end it, without `break`."}]}]}