{"id":"boolean-logic-operators","slug":"and-or-not","title":"And, or, not","text":"Conditions can be combined:\n\n- `a && b` (**and**) is true when both `a` and `b` are true.\n- `a || b` (**or**) is true when at least one of them is true.\n- `!a` (**not**) is true when `a` is false.\n\nA range check is the classic use of `&&`: a number is between 1 and 10 when it's at least 1 **and** at most 10.\n\n```java\nint number = 7;\nif (number >= 1 && number <= 10) {\n    System.out.println(\"In range\");\n}\nif (number < 1 || number > 10) {\n    System.out.println(\"Out of range\");\n}\nSystem.out.println(!(number > 5));\n```\n\n```output\nIn range\nfalse\n```\n\nJava checks `&&` and `||` from left to right and stops as soon as the answer is known: in `a && b`, when `a` is false, `b` isn't looked at.\n","task":"Read an age and, using a single `if` with `&&`, print `Teenager` for ages 13 to 19 and `Not a teenager` for any other age. For the input `15` the program prints:\n\n```\nTeenager\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 age = 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 age = Integer.valueOf(scanner.nextLine());\n        if (age >= 13 && age <= 19) {\n            System.out.println(\"Teenager\");\n        } else {\n            System.out.println(\"Not a teenager\");\n        }\n    }\n}\n","hints":["13 to 19 means at least 13 **and** at most 19.","`if (age >= 13 && age <= 19) { ... } else { ... }`"],"tests":[{"name":"Test 1","stdin":"15\n","expect":"Teenager","hidden":false},{"name":"Test 2","stdin":"13\n","expect":"Teenager","hidden":true},{"name":"Test 3","stdin":"19\n","expect":"Teenager","hidden":true},{"name":"Test 4","stdin":"20\n","expect":"Not a teenager","hidden":true},{"name":"Test 5","stdin":"12\n","expect":"Not a teenager","hidden":true}],"require":[{"pattern":"&&","message":"Combine the two checks with `&&`."},{"pattern":"\\bif\\b","max":1,"message":"Use a single `if` (with an `else`)."}],"forbid":[],"more":[{"task":"Read a temperature and print `Extreme temperature` when it's below -20 or above 40, and `OK` otherwise. Use `||`. For the input `45` the program prints:\n\n```\nExtreme temperature\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\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        if (temperature < -20 || temperature > 40) {\n            System.out.println(\"Extreme temperature\");\n        } else {\n            System.out.println(\"OK\");\n        }\n    }\n}\n","hints":["Either end is extreme: `temperature < -20 || temperature > 40`.","Exactly -20 and exactly 40 are still OK."],"tests":[{"name":"Test 1","stdin":"45\n","expect":"Extreme temperature","hidden":false},{"name":"Test 2","stdin":"-21\n","expect":"Extreme temperature","hidden":true},{"name":"Test 3","stdin":"40\n","expect":"OK","hidden":true},{"name":"Test 4","stdin":"-20\n","expect":"OK","hidden":true}],"require":[{"pattern":"\\|\\|","message":"Combine the two checks with `||`."}],"forbid":[]},{"task":"Read a number and print `Out of range` when it's not between 1 and 10, and `In range` otherwise. Write the condition with `!` in front of the range check, as `!( ... && ... )`. For the input `12` the program prints:\n\n```\nOut of range\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        if (!(number >= 1 && number <= 10)) {\n            System.out.println(\"Out of range\");\n        } else {\n            System.out.println(\"In range\");\n        }\n    }\n}\n","hints":["The range check is `number >= 1 && number <= 10`.","Put it in parentheses and `!` in front: `if (!(number >= 1 && number <= 10))`."],"tests":[{"name":"Test 1","stdin":"12\n","expect":"Out of range","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"In range","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"Out of range","hidden":true},{"name":"Test 4","stdin":"10\n","expect":"In range","hidden":true}],"require":[{"pattern":"!\\s*\\(","message":"Use `!` in front of the whole range check: `!( ... )`."},{"pattern":"&&","message":"Inside the parentheses, check the range with `&&`."}],"forbid":[]}]}