Null Statements

The simplest statement is the empty statement, also known as a null statement. A null statement is a single semicolon:

; // null statement

A null statement is useful where the language requires a statement but the program’s logic does not. Such usage is most common when a loop’s work can be done within its condition. For example, we might want to read an input stream, ignoring everything we read until we encounter a particular value:

// read until we hit end-of-file or find an input equal to soughtwhile (cin >> s && s != sought)    ; // null statement

This condition reads a value from the standard input and implicitly tests cin to see whether the read was successful. Assuming the read succeeded, the second part of the condition ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.