If there is only one statement in an if or else block, the braces are optional.
public class MainClass {
public static void main(String[] args) {
int a = 3;
if (a > 3)
a++;
else
a = 3;
}
}
Consider the following example:
public class MainClass {
public static void main(String[] args) {
int a = 3, b = 1;
if (a > 0 || b < 5)
if (a > 2)
System.out.println("a > 2");
else
System.out.println("a < 2");
}
}
It is hard to tell which if statement the else statement is associated with.
Actually, An else statement is always associated with the immediately preceding if.
Using braces makes your code clearer.
public class MainClass {
public static void main(String[] args) {
int a = 3, b = 1;
if (a > 0 || b < 5) {
if (a > 2) {
System.out.println("a > 2");
} else {
System.out.println("a < 2");
}
}
}
}