{"id":"conditionals-order","slug":"order-of-conditions","title":"The first true branch wins","text":"In an `else if` chain only the first true condition counts, so the **order** of the conditions changes what the program does. Look at this chain, meant to give a grade:\n\n```java\nint points = 95;\nif (points >= 50) {\n    System.out.println(\"Passed\");\n} else if (points >= 90) {\n    System.out.println(\"Excellent\");\n}\n```\n\n```output\nPassed\n```\n\n95 should be `Excellent`, but `points >= 50` is checked first, it's true, and the rest of the chain is skipped. `Excellent` can never be printed. Checking the strictest condition first fixes it:\n\n```java\nint points = 95;\nif (points >= 90) {\n    System.out.println(\"Excellent\");\n} else if (points >= 50) {\n    System.out.println(\"Passed\");\n}\n```\n\n```output\nExcellent\n```\n\nWhen conditions overlap, put the most specific one first. Each `else if` can then rely on the earlier conditions having been false: in the second version, the `>= 50` branch only ever sees points below 90.\n","task":"This chain is in the wrong order, so it never prints `Gold`. Reorder the branches, so 1000 points or more gives `Gold`, 500 or more `Silver`, and anything less `Bronze`. For the input `1200` the program prints `Gold`.\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        if (points >= 500) {\n            System.out.println(\"Silver\");\n        } else if (points >= 1000) {\n            System.out.println(\"Gold\");\n        } else {\n            System.out.println(\"Bronze\");\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 >= 1000) {\n            System.out.println(\"Gold\");\n        } else if (points >= 500) {\n            System.out.println(\"Silver\");\n        } else {\n            System.out.println(\"Bronze\");\n        }\n    }\n}\n","hints":["Every number that is at least 1000 is also at least 500, so the Silver check catches it first.","Swap the two conditions and their messages: check `>= 1000` first."],"tests":[{"name":"Test 1","stdin":"1200\n","expect":"Gold","hidden":false},{"name":"Test 2","stdin":"1000\n","expect":"Gold","hidden":true},{"name":"Test 3","stdin":"999\n","expect":"Silver","hidden":true},{"name":"Test 4","stdin":"20\n","expect":"Bronze","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read a score from 0 to 100 and print its band: 90 to 100 is `A`, 70 to 89 is `B`, 50 to 69 is `C`, and below 50 is `D`. For the input `75` the program prints:\n\n```\nB\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 score = 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 score = Integer.valueOf(scanner.nextLine());\n        if (score >= 90) {\n            System.out.println(\"A\");\n        } else if (score >= 70) {\n            System.out.println(\"B\");\n        } else if (score >= 50) {\n            System.out.println(\"C\");\n        } else {\n            System.out.println(\"D\");\n        }\n    }\n}\n","hints":["Start with the highest band, so each later branch only sees lower scores.","`if (score >= 90)`, then `else if (score >= 70)`, then `else if (score >= 50)`, then `else`."],"tests":[{"name":"Test 1","stdin":"75\n","expect":"B","hidden":false},{"name":"Test 2","stdin":"90\n","expect":"A","hidden":true},{"name":"Test 3","stdin":"69\n","expect":"C","hidden":true},{"name":"Test 4","stdin":"0\n","expect":"D","hidden":true}],"require":[],"forbid":[]},{"task":"A finer scale, and a check on the input. Scores go from 0 to 100; anything else is `Invalid score`. The grades (an invented scale for this exercise):\n\n| Score | Grade |\n|---|---|\n| 95 to 100 | `A+` |\n| 85 to 94 | `A` |\n| 75 to 84 | `B` |\n| 65 to 74 | `C` |\n| 55 to 64 | `D` |\n| 45 to 54 | `E` |\n| 0 to 44 | `F` |\n\nFor the input `88` the program prints:\n\n```\nA\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 score = 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 score = Integer.valueOf(scanner.nextLine());\n        if (score > 100) {\n            System.out.println(\"Invalid score\");\n        } else if (score >= 95) {\n            System.out.println(\"A+\");\n        } else if (score >= 85) {\n            System.out.println(\"A\");\n        } else if (score >= 75) {\n            System.out.println(\"B\");\n        } else if (score >= 65) {\n            System.out.println(\"C\");\n        } else if (score >= 55) {\n            System.out.println(\"D\");\n        } else if (score >= 45) {\n            System.out.println(\"E\");\n        } else if (score >= 0) {\n            System.out.println(\"F\");\n        } else {\n            System.out.println(\"Invalid score\");\n        }\n    }\n}\n","hints":["Check for scores over 100 first, then go down the bands from the top.","What's left after `score >= 0` is negative: that's invalid too, so the final `else` prints `Invalid score`."],"tests":[{"name":"Test 1","stdin":"88\n","expect":"A","hidden":false},{"name":"Test 2","stdin":"101\n","expect":"Invalid score","hidden":true},{"name":"Test 3","stdin":"-1\n","expect":"Invalid score","hidden":true},{"name":"Test 4","stdin":"95\n","expect":"A+","hidden":true},{"name":"Test 5","stdin":"44\n","expect":"F","hidden":true},{"name":"Test 6","stdin":"65\n","expect":"C","hidden":true}],"require":[],"forbid":[]}]}