In a textbook I've been looking through for a class, it is stated, a statement can be empty (the null statement). The null statement is defined as just a semicolon.
It does absolutely nothing at execution time. The null statement is rarely used.
As a moderately experienced programmer, I find it interesting that they say rarely used, is this implies that somewhere, there is a use for it.
What could possibly be the use of such a statement?
Best Answer
It is mostly useless, but there are a few places where it is necessary. For example:
This executes the
DoSomething()
method until it returns false. Obviously this assumes the method also performs some useful side-effect. But the empty statement, i.e. the trailing semicolon, is required, otherwise the while-loop would include the next following statement instead.(But to contradict the other answers,
for(;;) {}
is not an example of empty statements, since the parenthesis after a for clause does not contain statements, it just uses the semicolon to separate (optional) expressions. See the Java grammar for details.)