5.1. Simple Statements

Image

Most statements in C++ end with a semicolon. An expression, such as ival + 5, becomes an expression statement when it is followed by a semicolon. Expression statements cause the expression to be evaluated and its result discarded:

ival + 5;      // rather useless expression statementcout << ival;  // useful expression statement

The first statement is pretty useless: The addition is done but the result is not used. More commonly, an expression statement contains an expression that has a side effect—such as assigning a new value to a variable, or printing a result—when it is evaluated.

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.