{"id":"conditionals-if","slug":"if-statements","title":"if","text":"So far every line of a program ran every time. An `if` statement runs a block of code only when a **condition** is true:\n\n```java\nint temperature = 31;\nif (temperature > 30) {\n    System.out.println(\"Hot day: drink water.\");\n}\nSystem.out.println(\"Temperature: \" + temperature);\n```\n\n```output\nHot day: drink water.\nTemperature: 31\n```\n\n- The condition goes in parentheses after `if`. Here it's `temperature > 30`, which is either true or false.\n- The **block** in braces `{ }` runs only when the condition is true. With `temperature = 25`, the program would print only the second line.\n- The line after the block runs either way.\n\n### No semicolon after the condition\n\nA semicolon right after `if (...)` ends the `if` there, with nothing in it. The block below it then runs every time, and Java doesn't warn about it:\n\n```java\nint speed = 30;\nif (speed > 50); {\n    System.out.println(\"Too fast!\");\n}\n```\n\n```output\nToo fast!\n```\n\nOnly 30, and yet \"Too fast!\". When an `if` seems to be ignored, check for this semicolon first.\n","task":"Fill in the condition, so the program prints `Too heavy for the cabin.` only when the bag weighs over 8 kilograms. Every run ends with `Have a good flight.`\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 weight = Integer.valueOf(scanner.nextLine());\n        if ([[weight > 8‖weight >= 9‖8 < weight]]) {\n            System.out.println(\"Too heavy for the cabin.\");\n        }\n        System.out.println(\"Have a good flight.\");\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 weight = Integer.valueOf(scanner.nextLine());\n        if (weight > 8) {\n            System.out.println(\"Too heavy for the cabin.\");\n        }\n        System.out.println(\"Have a good flight.\");\n    }\n}\n","hints":["The condition compares `weight` with 8 using `>`.","Exactly 8 is not over 8, so `weight > 8` is right and `weight >= 8` isn't."],"tests":[{"name":"Test 1","stdin":"12\n","expect":"Too heavy for the cabin.\nHave a good flight.","hidden":false},{"name":"Test 2","stdin":"8\n","expect":"Have a good flight.","hidden":true},{"name":"Test 3","stdin":"5\n","expect":"Have a good flight.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read a temperature and print `Hot day: drink water.` when it's over 30. Then always print the temperature. For the input `31` the program prints:\n\n```\nHot day: drink water.\nTemperature: 31\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 > 30) {\n            System.out.println(\"Hot day: drink water.\");\n        }\n        System.out.println(\"Temperature: \" + temperature);\n    }\n}\n","hints":["The warning goes inside an `if` block; the temperature line goes after it.","`if (temperature > 30) { ... }`"],"tests":[{"name":"Test 1","stdin":"31\n","expect":"Hot day: drink water.\nTemperature: 31","hidden":false},{"name":"Test 2","stdin":"30\n","expect":"Temperature: 30","hidden":true},{"name":"Test 3","stdin":"-5\n","expect":"Temperature: -5","hidden":true}],"require":[],"forbid":[]},{"task":"Two separate checks on one number. Print `Big number` when it's over 100 and `Negative number` when it's below 0, then always `Done`. For the input `250` the program prints:\n\n```\nBig number\nDone\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 > 100) {\n            System.out.println(\"Big number\");\n        }\n        if (number < 0) {\n            System.out.println(\"Negative number\");\n        }\n        System.out.println(\"Done\");\n    }\n}\n","hints":["Two `if` statements, one after the other, each with its own block.","`number < 0` is true for negative numbers."],"tests":[{"name":"Test 1","stdin":"250\n","expect":"Big number\nDone","hidden":false},{"name":"Test 2","stdin":"-3\n","expect":"Negative number\nDone","hidden":true},{"name":"Test 3","stdin":"50\n","expect":"Done","hidden":true}],"require":[],"forbid":[]}]}