{"id":"boolean-logic-order","slug":"specific-first","title":"Most specific condition first","text":"When conditions overlap, the order of an `else if` chain decides the answer, and the most specific case has to come first. The classic example is **FizzBuzz**: print `Fizz` for multiples of 3, `Buzz` for multiples of 5, `FizzBuzz` for multiples of both, and the number itself otherwise.\n\n```java\nint number = 15;\nif (number % 3 == 0 && number % 5 == 0) {\n    System.out.println(\"FizzBuzz\");\n} else if (number % 3 == 0) {\n    System.out.println(\"Fizz\");\n} else if (number % 5 == 0) {\n    System.out.println(\"Buzz\");\n} else {\n    System.out.println(number);\n}\n```\n\n```output\nFizzBuzz\n```\n\n15 is a multiple of 3, so if the `number % 3 == 0` check came first, it would print `Fizz` and never reach the combined case. Checking \"both\" first works, because every later branch can then assume that \"both\" was false.\n","task":"Read a number and print `FizzBuzz`, `Fizz`, `Buzz` or the number itself, by the rules above. For the input `9` the program prints:\n\n```\nFizz\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 % 3 == 0 && number % 5 == 0) {\n            System.out.println(\"FizzBuzz\");\n        } else if (number % 3 == 0) {\n            System.out.println(\"Fizz\");\n        } else if (number % 5 == 0) {\n            System.out.println(\"Buzz\");\n        } else {\n            System.out.println(number);\n        }\n    }\n}\n","hints":["The combined case goes first, before the checks for 3 and for 5.","The last `else` prints the number itself."],"tests":[{"name":"Test 1","stdin":"9\n","expect":"Fizz","hidden":false},{"name":"Test 2","stdin":"15\n","expect":"FizzBuzz","hidden":true},{"name":"Test 3","stdin":"10\n","expect":"Buzz","hidden":true},{"name":"Test 4","stdin":"7\n","expect":"7","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read a year and print `Leap year` or `Not a leap year`. The rules of the calendar we use:\n\n- a year divisible by 4 is a leap year,\n- except a year divisible by 100, which is not,\n- except a year divisible by 400, which is.\n\nFor the input `2024` the program prints `Leap year`.\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 year = 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 year = Integer.valueOf(scanner.nextLine());\n        if (year % 400 == 0) {\n            System.out.println(\"Leap year\");\n        } else if (year % 100 == 0) {\n            System.out.println(\"Not a leap year\");\n        } else if (year % 4 == 0) {\n            System.out.println(\"Leap year\");\n        } else {\n            System.out.println(\"Not a leap year\");\n        }\n    }\n}\n","hints":["Start with the most specific rule, divisible by 400, then 100, then 4.","Hidden tests include 1900 (not a leap year) and 2000 (a leap year)."],"tests":[{"name":"Test 1","stdin":"2024\n","expect":"Leap year","hidden":false},{"name":"Test 2","stdin":"1900\n","expect":"Not a leap year","hidden":true},{"name":"Test 3","stdin":"2000\n","expect":"Leap year","hidden":true},{"name":"Test 4","stdin":"2023\n","expect":"Not a leap year","hidden":true}],"require":[],"forbid":[]},{"task":"Read a whole number and classify it:\n\n- divisible by both 2 and 3: `Divisible by 6`\n- divisible by 2 only: `Even`\n- divisible by 3 only: `Multiple of three`\n- anything else: `Other`\n\nFor the input `12` the program prints `Divisible by 6`.\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 % 2 == 0 && number % 3 == 0) {\n            System.out.println(\"Divisible by 6\");\n        } else if (number % 2 == 0) {\n            System.out.println(\"Even\");\n        } else if (number % 3 == 0) {\n            System.out.println(\"Multiple of three\");\n        } else {\n            System.out.println(\"Other\");\n        }\n    }\n}\n","hints":["The same shape as FizzBuzz, with 2 and 3.","Check the combined case first: `number % 2 == 0 && number % 3 == 0`."],"tests":[{"name":"Test 1","stdin":"12\n","expect":"Divisible by 6","hidden":false},{"name":"Test 2","stdin":"8\n","expect":"Even","hidden":true},{"name":"Test 3","stdin":"9\n","expect":"Multiple of three","hidden":true},{"name":"Test 4","stdin":"25\n","expect":"Other","hidden":true}],"require":[],"forbid":[]}]}