{"id":"conditionals-else","slug":"else","title":"else","text":"`else` adds a second block that runs when the condition is **false**. Exactly one of the two blocks runs, never both, never neither:\n\n```java\nint balance = -4;\nif (balance >= 0) {\n    System.out.println(\"Balance OK\");\n} else {\n    System.out.println(\"Overdrawn\");\n}\n```\n\n```output\nOverdrawn\n```\n\n`else` has no condition of its own: it simply catches every case the `if` didn't. The `} else {` line closes the first block and opens the second, so it lines up with the `if`.\n","task":"Read the hour of the day (0 to 23) and print `Good morning` when it's before 12, and `Good afternoon` otherwise. For the input `9` the program prints:\n\n```\nGood morning\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 hour = 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 hour = Integer.valueOf(scanner.nextLine());\n        if (hour < 12) {\n            System.out.println(\"Good morning\");\n        } else {\n            System.out.println(\"Good afternoon\");\n        }\n    }\n}\n","hints":["`if (hour < 12) { ... } else { ... }`","12 is not before 12, so at noon it's already \"Good afternoon\"."],"tests":[{"name":"Test 1","stdin":"9\n","expect":"Good morning","hidden":false},{"name":"Test 2","stdin":"12\n","expect":"Good afternoon","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"Good morning","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read an age and print `Adult` for 18 or older, otherwise `Not yet an adult`. For the input `18` the program prints:\n\n```\nHow old are you?\nAdult\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        System.out.println(\"How old are you?\");\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        System.out.println(\"How old are you?\");\n        int age = Integer.valueOf(scanner.nextLine());\n        if (age >= 18) {\n            System.out.println(\"Adult\");\n        } else {\n            System.out.println(\"Not yet an adult\");\n        }\n    }\n}\n","hints":["18 or older is `age >= 18`.","A hidden test tries 17, the last age that isn't adult."],"tests":[{"name":"Test 1","stdin":"18\n","expect":"How old are you?\nAdult","hidden":false},{"name":"Test 2","stdin":"17\n","expect":"How old are you?\nNot yet an adult","hidden":true},{"name":"Test 3","stdin":"40\n","expect":"How old are you?\nAdult","hidden":true}],"require":[],"forbid":[]},{"task":"Read a number of points. With 50 or more, print `Passed with N points`; otherwise `N points is not enough, 50 needed`. For the input `72` the program prints:\n\n```\nPassed with 72 points\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 points = 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 points = Integer.valueOf(scanner.nextLine());\n        if (points >= 50) {\n            System.out.println(\"Passed with \" + points + \" points\");\n        } else {\n            System.out.println(points + \" points is not enough, 50 needed\");\n        }\n    }\n}\n","hints":["Both messages include the number, joined with `+`.","`if (points >= 50) { ... } else { ... }`"],"tests":[{"name":"Test 1","stdin":"72\n","expect":"Passed with 72 points","hidden":false},{"name":"Test 2","stdin":"49\n","expect":"49 points is not enough, 50 needed","hidden":true},{"name":"Test 3","stdin":"50\n","expect":"Passed with 50 points","hidden":true}],"require":[],"forbid":[]}]}