Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Indentation
Java Basics Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Basics Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.
-->
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
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. Keep in mind, we will cover if-else statements later in the course.
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.