{"id":"conditionals-comparisons","slug":"comparisons","title":"Comparison operators","text":"A condition is usually a comparison. Java has six comparison operators, and each one gives `true` or `false`:\n\n| Operator | Means | Example that's true |\n|---|---|---|\n| `>` | greater than | `5 > 3` |\n| `>=` | greater than or equal to | `3 >= 3` |\n| `<` | less than | `2 < 7` |\n| `<=` | less than or equal to | `7 <= 7` |\n| `==` | equal to | `4 == 4` |\n| `!=` | not equal to | `4 != 5` |\n\n### `==` compares, `=` assigns\n\nComparing uses two equals signs. With one, Java reads it as an assignment. An assignment has a value too, the value it stores: here that's the `int` 5, and an `if` needs `true` or `false`:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int number = 5;\n        if (number = 5) {\n            System.out.println(\"Five!\");\n        }\n    }\n}\n```\n\n```javac\nMain.java:4: error: incompatible types: int cannot be converted to boolean\n        if (number = 5) {\n                   ^\n1 error\n```\n\nWith numbers the compiler catches this mistake. With a `boolean` variable (the next module introduces them) it can't: `if (done = true)` stores `true` in `done`, compiles without a warning, and the block always runs. To compare, always use `==`.\n\nThese operators compare numbers. Comparing text is different, and the next module shows how.\n","task":"Read a number and print `Correct!` when it's exactly 7. Then always print `Game over.` For the input `7` the program prints:\n\n```\nGuess the number:\nCorrect!\nGame over.\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(\"Guess the number:\");\n        int guess = 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        System.out.println(\"Guess the number:\");\n        int guess = Integer.valueOf(scanner.nextLine());\n        if (guess == 7) {\n            System.out.println(\"Correct!\");\n        }\n        System.out.println(\"Game over.\");\n    }\n}\n","hints":["Equal to is `==`, with two equals signs.","`if (guess == 7) { ... }`, then the last line after the block."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"Guess the number:\nCorrect!\nGame over.","hidden":false},{"name":"Test 2","stdin":"8\n","expect":"Guess the number:\nGame over.","hidden":true},{"name":"Test 3","stdin":"-7\n","expect":"Guess the number:\nGame over.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read a year and print `That's before Unix time began.` when it's earlier than 1970. (Unix time, which many computers use to count time, starts on 1 January 1970.) Always finish with `Thanks!` For the input `1969` the program prints:\n\n```\nYear:\nThat's before Unix time began.\nThanks!\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(\"Year:\");\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        System.out.println(\"Year:\");\n        int year = Integer.valueOf(scanner.nextLine());\n        if (year < 1970) {\n            System.out.println(\"That's before Unix time began.\");\n        }\n        System.out.println(\"Thanks!\");\n    }\n}\n","hints":["Earlier than 1970 means less than: `year < 1970`.","A hidden test tries 1970 itself, which is not earlier."],"tests":[{"name":"Test 1","stdin":"1969\n","expect":"Year:\nThat's before Unix time began.\nThanks!","hidden":false},{"name":"Test 2","stdin":"1970\n","expect":"Year:\nThanks!","hidden":true},{"name":"Test 3","stdin":"2000\n","expect":"Year:\nThanks!","hidden":true}],"require":[],"forbid":[]},{"task":"Read a status code. When it isn't 0, print `Error code: ` and the code. When it's 500 or more, also print `Server error`. Always finish with `Checked.` For the input `503` the program prints:\n\n```\nError code: 503\nServer error\nChecked.\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 code = 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 code = Integer.valueOf(scanner.nextLine());\n        if (code != 0) {\n            System.out.println(\"Error code: \" + code);\n        }\n        if (code >= 500) {\n            System.out.println(\"Server error\");\n        }\n        System.out.println(\"Checked.\");\n    }\n}\n","hints":["Not equal is `!=`: `if (code != 0) { ... }`.","500 or more is `code >= 500`, in a second `if`."],"tests":[{"name":"Test 1","stdin":"503\n","expect":"Error code: 503\nServer error\nChecked.","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Checked.","hidden":true},{"name":"Test 3","stdin":"404\n","expect":"Error code: 404\nChecked.","hidden":true},{"name":"Test 4","stdin":"500\n","expect":"Error code: 500\nServer error\nChecked.","hidden":true}],"require":[],"forbid":[]}]}