Java control statements (if, for, while, etc.) may be executed even if the curly braces are omitted. If {} is omitted, only one line is processed when true.
In the code of the following example (1), if the result of the conditional expression (i <3) is true, the only statement to be executed is the statement of process 1. The statement of process 2 is always executed regardless of the conditional expression.
Example ①
if(i < 3)
System.out.println("Process 1");
System.out.println("Process 2");
In other words, Example ① behaves the same as the code in Example ② below.
Example ②
if(i < 3){
System.out.println("Process 1");
}
System.out.println("Process 2");
This time, I made a note because there was a possibility of taking the Java qualification test. However, although there is no grammatical problem, readability and maintainability are reduced, so basically it is not omitted and it is in the form of a block {} as shown in Example ②.
Recommended Posts