{"id":"conditionals-indentation","slug":"indentation","title":"Blocks and indentation","text":"A **block** is the code between a `{` and its `}`. The class, the main method and each `if` have one, and blocks sit inside each other.\n\nJava itself doesn't care about spaces at the start of a line. People do: indentation shows at a glance which lines are inside which block. The usual rule, used throughout this site:\n\n- every line inside a block is indented **4 spaces** more than the line that opened the block;\n- the closing `}` lines up with the line that opened the block.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int points = 12;\n        if (points > 10) {\n            System.out.println(\"Level up!\");\n        }\n        System.out.println(\"Points: \" + points);\n    }\n}\n```\n\n```output\nLevel up!\nPoints: 12\n```\n\nThe editor helps: after a `{`, pressing Enter indents the next line, and the **Tab** key (also on the symbol bar on phones) adds 4 spaces.\n\n### Checking the style\n\nIn the first two challenges of this step, **Check** also checks the indentation. Everywhere else, indentation that doesn't match the braces shows up as a note after the tests, but doesn't stop a challenge from passing.\n","task":"This program works, but its indentation is a mess. Re-indent every line so it follows the rule. The output stays:\n\n```\nWarm day\nHave a nice day\n```\n","kind":"code","seed":"public class Main {\npublic static void main(String[] args) {\nint temperature = 25;\n  if (temperature > 20) {\nSystem.out.println(\"Warm day\");\n    }\n System.out.println(\"Have a nice day\");\n}\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int temperature = 25;\n        if (temperature > 20) {\n            System.out.println(\"Warm day\");\n        }\n        System.out.println(\"Have a nice day\");\n    }\n}\n","hints":["Count the blocks around each line: `public static void main` is inside 1 block (4 spaces), `int temperature` inside 2 (8 spaces).","`System.out.println(\"Warm day\");` is inside 3 blocks: 12 spaces. Each `}` lines up with the line that opened its block."],"tests":[{"name":"Output","stdin":"","expect":"Warm day\nHave a nice day","hidden":false}],"require":[],"forbid":[],"style":"indent","more":[{"task":"Re-indent this program too. It reads a number and checks it twice. For the input `250` the output stays:\n\n```\nBig number\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 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(\"Checked\");\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(\"Checked\");\n    }\n}\n","hints":["Inside main (2 blocks deep) lines have 8 spaces; inside an `if` in main, 12.","Both `if` statements are at the same level, so they start in the same column."],"tests":[{"name":"Test 1","stdin":"250\n","expect":"Big number\nChecked","hidden":false},{"name":"Test 2","stdin":"-8\n","expect":"Negative number\nChecked","hidden":true},{"name":"Test 3","stdin":"7\n","expect":"Checked","hidden":true}],"require":[],"forbid":[],"style":"indent"},{"task":"The indentation in this program makes it look as if both extra lines belong to the inner `if`, but only the first one does: an `if` without braces controls just one statement. Add braces, so both lines are printed only when the points are over 20. For the input `25` the program prints:\n\n```\nLevel up!\nBonus!\nDouble bonus!\n```\n\nFor `15` it prints only `Level up!`.\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 > 10) {\n            System.out.println(\"Level up!\");\n            if (points > 20)\n                System.out.println(\"Bonus!\");\n                System.out.println(\"Double bonus!\");\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 > 10) {\n            System.out.println(\"Level up!\");\n            if (points > 20) {\n                System.out.println(\"Bonus!\");\n                System.out.println(\"Double bonus!\");\n            }\n        }\n    }\n}\n","hints":["Without braces, `if (points > 20)` only controls the next statement, `Bonus!`. `Double bonus!` runs whenever the points are over 10.","Put `{` after `if (points > 20)` and a matching `}` after the `Double bonus!` line."],"tests":[{"name":"Test 1","stdin":"25\n","expect":"Level up!\nBonus!\nDouble bonus!","hidden":false},{"name":"Test 2","stdin":"15\n","expect":"Level up!","hidden":true},{"name":"Test 3","stdin":"20\n","expect":"Level up!","hidden":true}],"require":[],"forbid":[]}]}