Omission of curly braces in if statement (Java silver)

Omission of curly braces in if statement

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

Omission of curly braces in if statement (Java silver)
Basics of java basics ② ~ if statement and switch statement ~
Implementation of gzip in java
Implementation of tri-tree in Java
Summary of [Java silver study] package
Try an If expression in Java
List of members added in Java 9
Java study # 4 (conditional branching / if statement)
List of types added in Java 9
Implementation of like function in Java
The story of acquiring Java Silver in two months from completely inexperienced.
[Java] I participated in ABC-188 of Atcorder.
[Java Silver] Summary of access modifier points
Implementation of DBlayer in Java (RDB, MySQL)
Get the result of POST in Java
Java, if statement / switch statement starting from beginner
[Ruby] if statement concept of conditional expression
The story of writing Java in Emacs
Role of JSP in Web application [Java]
Discrimination of Enums in Java 7 and above
if statement
Conditional branching of the result of SQL statement to search only one in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
Be careful if you find SHIFT-JIS in Java
The story of learning Java in the first programming
[Java] use Optional instead of if (hoge! = null) {...}
Measure the size of a folder in Java
[Java] Use of final in local variable declaration
Second decoction: Try an If expression in Java
Feel the passage of time even in Java
Basics of threads and Callable in Java [Beginner]
A quick review of Java learned in class
Method name of method chain in Java Builder + α
[Ruby] What if you put each statement in each statement? ??
Import files of the same hierarchy in Java
Why use setters/getters instead of public/private in Java