JAVA.STMT.IFELSE.BLOCK

The body of an if/else statement must be a block between {and}.

Vulnerability and risk

Omitting the curly braces in the body of an if/else statement can result in inadvertent programming errors.

Mitigation and prevention

Use curly braces in the body of the if/else statement.

Example 1

Copy
public class C18 {
    
    public void doSomething() {
        
    }
    
    public void testNGIf(int some_number) {
        
        if (some_number == 1)
            doSomething(); //NG
        else if (some_number == 2)
            doSomething(); //NG
        else
            doSomething(); //NG
        
        
        if (some_number == 3); //NG
        else if (some_number == 4); //NG
        else; //NG
        
    }
    
    public void testOKIf(int some_number) {
        
        if (some_number == 1) {
            doSomething();
        } else if (some_number == 2) {
            doSomething();
        } else {
            doSomething();
        }
    }
    
}