Indentation

Basic Syntax

Indentation

🙋 Need help? Ask an expert now!

A new Java programmer must master the concept of "indentation" in Java. Indentation is used to make our code readable to other users, easier to edit, display how the braces match up and show the logic of the program in an organized fashion. Please look at the example below to see how indentation of the if-else statement makes it easy to see what is happening.

IndentationExample.java
package exlcode;

public class IndentationExample {

  // standard indenting increases by 2 spaces as shown below
  // braces are used even when optional
  public static void main(String[] args) {
    // neat indenting:
    // if-else will be explained in Unit 5
    if (true) {
      System.out.println("True");
    } else {
      System.out.println("False");
    }

    // no indenting:
    if(true)          System.out.println("True");
    else    System.out.println("False");

  }
}

The goal of indentation is to show the logic of our program. Keeping indentation consistent throughout a program is essential. For statements inside a left and right brace, increase the indenting by two spaces. When another pair of braces is nested inside those braces, increase the indentation by another two spaces. It is a good idea to line up the braces vertically. Keep in mind that the function of indentation is to make the program more readable and understandable, which saves an immense amount of time when editing or refining your code.

Edit Me on GitHub!

Application Question

Which one of the choices below best explain why indentation is important when programming?