O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required