2.6. Control Flow Constructs

C++ has constructs that dictate which statements execute, based on the values of expressions. This section introduces C++ constructs and describes how they affect the logic and control flow of programs.

if and if-else

The if construct has three formats. The first format is

if (expression) {
   statements;                 // if-block
}

The expression requires parentheses. If expression evaluates to true, statements execute and control continues with the next statement following the if-block. If expression evaluates to false, control skips to the statement following the if-block. Braces are optional if there is only one statement in the if-block.

The second format is

if (expression) {
   statements1;
}
else {
   statements2;
}

If expression ...

Get Navigating C++ and Object-Oriented Design 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.