Empty Statement in Java
In Java, there are three types of statements that are declaration, expression, and control statements. Beside this, there is another statement is known as empty statement. In this section, we will discuss about the empty statement in Java with examples.
As the name suggests, an empty statement does not have anything barring semicolon (;).
It can be used in various cases. Let's discuss a few of them.
Empty Statement in a for Loop
Therefore, we have to avoid making the scope of the variable k local. The following code snippet does the same.
Here, the initialization block of the for loop contains nothing, hence an empty statement.
For creating a loop that runs infinitely, we can use empty statements.
However, if we use break statements inside the body of the loop, then the loop can terminate.
If we want to create a for-loop or while loop that has an empty body, we can use an empty statement. Let's start with for-loop.
The semicolon immediately after the for loop shows that the body of the for loop is empty.
The above for-loop can also be written as:
We can have multiple empty statements inside the body of the for loop.
Here, we have created 10 empty statements inside the body of the for loop.
Empty Statement in while Loop
Similar to for-loop, we can also create empty statements using the while loop.
We have created a while loop whose body contains an empty statement, which is equivalent to
Multiple empty statements can also be inserted inside the body of the loop.
Note that multiple empty statements do not make any difference.
Empty Statement in if … else Statement
In the above code snippet, the if the condition has the empty body (or statement) as we have put a semicolon immediately after the if statement. Similar to loops, we can re-write the above code snippet as:
Similarly, we can put semicolon immediately after else statement. Observe the following code.
Here also, we have removed the curly braces for the else part.
Some Common Mistakes
There are some common mistakes that occur using empty statements. A few of them are mentioned below.
1) If we put a pair of semicolons just after the if statement and then write the else statement, it gives the compilation error.
/EmptyStmnt.java:7: error: 'else' without 'if' else;
Explanation: It is because a pair of semicolons that we put after the if condition. The first semicolon represents the empty body of the if condition, and the second semicolon goes out of the body of the if condition.
Thus, we have an empty statement that is present between the if part and the else part. It is a known fact that one cannot put any statement (even though it is empty) in between the if and else part. Hence, the code will throw that compilation error if one wants to put two empty statements in if part one has to use the curly braces.
Explanation: Here, the two empty statements are the part of the if block, and immediately after the if block, else statement starts. Hence, the compiler does not complain.
2) Another common mistake one does; is to start the curly braces after putting the semicolon in the if … else block or in the loops too.
Explanation: The curly braces after the if part is not the part of the if part. It is because of the semicolon that comes after the if part. Thus, even though the condition (k > 0) is false, the print statement in the curly braces executes, and Hello is printed.
Let's see the same thing in for-loop.
Explanation: The curly braces after the for-loop is not the part of for-loop. Hence, the word Hello is printed only once.
A Word of Caution
Because of the above scenarios, it is required to use the semicolon (an empty statement) around if … else or around the loop carefully. Because usage of the semicolon will usually not raise any error or warning.
However, it will also not give the desired output. Since any exception or error is not raised, spotting such a kind of error is a tedious task, especially when we are dealing with codes that contain hundreds or thousands of lines.