JAVA.STMT.WHILE.BLOCK

The body of the while statement must be a block enclosed by {and}.

Vulnerability and risk

If you omit the curly braces in the body of a while statement, a careless programming error may occur.

Mitigation and prevention

Use curly braces in the body of a while statement.

Example 1

Copy
public class C18 {
        
    public void testNGWhile(int some_number) {
        
        int i = 0;
        while (i < some_number) //NG
            i++;
        
        int j = 0;
        while (j < some_number); //NG
        
    }
    
    public void testOKWhile(int some_number) {
        
        int i = 0;
        while (i < some_number) {
            i++;
        }
    }
}