{"id":"boolean-logic-variables","slug":"boolean-conditions","title":"Booleans in conditions","text":"A comparison like `age >= 18` is an expression with a value, and that value is a `boolean`: `true` or `false`. It can be stored in a variable, printed, or used later as a condition:\n\n```java\nint age = 20;\nboolean adult = age >= 18;\nSystem.out.println(\"Adult: \" + adult);\nif (adult) {\n    System.out.println(\"Welcome in.\");\n}\n```\n\n```output\nAdult: true\nWelcome in.\n```\n\n`if (adult)` reads naturally: the condition is the variable itself, so there's no need to write `if (adult == true)`.\n\nA well-named boolean variable makes a condition easier to read, especially when the test is long or used more than once. Names like `adult`, `isRaining` or `hasTicket` work well: they read like yes-or-no questions.\n","task":"Fill in the blank, so `canVote` is `true` for an age of 18 or more. For the input `20` the program prints `Can vote: true`.\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        int age = Integer.valueOf(scanner.nextLine());\n        boolean canVote = [[age >= 18‖age > 17‖18 <= age]];\n        System.out.println(\"Can vote: \" + canVote);\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 age = Integer.valueOf(scanner.nextLine());\n        boolean canVote = age >= 18;\n        System.out.println(\"Can vote: \" + canVote);\n    }\n}\n","hints":["The blank is a comparison, the same you'd write inside an `if`.","18 or more is `age >= 18`."],"tests":[{"name":"Test 1","stdin":"20\n","expect":"Can vote: true","hidden":false},{"name":"Test 2","stdin":"18\n","expect":"Can vote: true","hidden":true},{"name":"Test 3","stdin":"17\n","expect":"Can vote: false","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read whether the customer is a member (`true` or `false`) into a boolean. Use the variable itself as the condition: members get `Discount applied`, others `Full price`. For the input `true` the program prints:\n\n```\nDiscount applied\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        boolean member = Boolean.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        boolean member = Boolean.valueOf(scanner.nextLine());\n        if (member) {\n            System.out.println(\"Discount applied\");\n        } else {\n            System.out.println(\"Full price\");\n        }\n    }\n}\n","hints":["`if (member)` is enough: the variable is already true or false.","Add an `else` for the full price."],"tests":[{"name":"Test 1","stdin":"true\n","expect":"Discount applied","hidden":false},{"name":"Test 2","stdin":"false\n","expect":"Full price","hidden":true},{"name":"Test 3","stdin":"TRUE\n","expect":"Discount applied","hidden":true}],"require":[{"pattern":"if\\s*\\(\\s*member\\s*\\)","message":"Use the variable itself as the condition: `if (member)`."}],"forbid":[]},{"task":"Read a temperature (an `int`) and whether it's raining (`true` or `false`). Store `temperature > 20` in a boolean called `warm`. Then print `Wear a T-shirt` when it's warm and `Wear a sweater` otherwise, and add `Take an umbrella` when it's raining. For the input `24` and `true` the program prints:\n\n```\nWear a T-shirt\nTake an umbrella\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 temperature = Integer.valueOf(scanner.nextLine());\n        boolean raining = Boolean.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 temperature = Integer.valueOf(scanner.nextLine());\n        boolean raining = Boolean.valueOf(scanner.nextLine());\n        boolean warm = temperature > 20;\n        if (warm) {\n            System.out.println(\"Wear a T-shirt\");\n        } else {\n            System.out.println(\"Wear a sweater\");\n        }\n        if (raining) {\n            System.out.println(\"Take an umbrella\");\n        }\n    }\n}\n","hints":["`boolean warm = temperature > 20;` stores the comparison.","Two separate decisions: `if (warm) ... else ...`, then `if (raining) ...`."],"tests":[{"name":"Test 1","stdin":"24\ntrue\n","expect":"Wear a T-shirt\nTake an umbrella","hidden":false},{"name":"Test 2","stdin":"20\nfalse\n","expect":"Wear a sweater","hidden":true},{"name":"Test 3","stdin":"3\ntrue\n","expect":"Wear a sweater\nTake an umbrella","hidden":true}],"require":[{"pattern":"boolean\\s+warm\\s*=","message":"Store the comparison in a boolean called `warm`."}],"forbid":[]}]}