A null statement is used to provide a null operation in situations where the grammar of the language requires a statement, but the program requires no work to be done. The null statement consists of a semicolon:
The null statement is useful with the if
,
while
, do
, and for
statements. The most common use of this statement is in loop
operations in which all the loop activity is performed by the test
portion of the loop. For example, the following statement finds the
first element of an array that has a value of 0:
for (i=0; array[i] != 0; i++) ;
In this example, the for
statement is executed for
its side effects only; the loop body is a null statement. See Section 7.6 for more information
about iteration statements.
The null statement is also useful where a label is needed just before a brace that terminates a compound statement. (A label cannot immediately precede the right brace; it must always be attached to a statement.) For example:
if (expression1) { ... goto label_1; /* Terminates this part of the if statement */ ... label_1: ; } else ...