Because a null statement is a statement, it is legal anywhere a statement is expected. For this reason, semicolons that might appear illegal are often nothing more than null statements. The following fragment contains two statements—the expression statement and the null statement:
ival = v1 + v2;; // ok: second semicolon is a superfluous null statement
Although an unnecessary null statement is often harmless, an extra semicolon following the condition in a while
or if
can drastically alter the programmer’s intent. For example, the following code will loop indefinitely:
// disaster: extra semicolon: loop body is this null statement
while (iter != svec.end()) ; // the while body is the empty statement
++iter; // increment is not part of the loop
Contrary to the indentation, the increment is not part of the loop. The loop body is the null statement formed by the semicolon that follows the condition.