3.4. Branching Overview

A branch, in whatever form it takes, is a decision point where the program chooses between two or more distinct paths through its executable code. Branches may take a short side trip, begin two divergent paths, or form numerous alternatives opening up like the roads spanning out from a city center. Regardless of how many paths lie ahead or where they may lead, a logical expression determines which branch or fork the computer follows. Although branches are simple structures, sequencing and nesting them results in programs of exquisite complexity and infinite variety. C++ provides two main branching structures: if-statements and switches. Both structures provide numerous options, explored in the following sections.

Simple Branch (Fork)
if-statement
Multi-way Branch
switch-statement
A single arrow, representing a single flow of control, splits into two possible flows. A test expression directs the program to take one flow or branch. A single arrow, representing a single flow of controls, has multiple paths leading away. The program selects a branch or path with a simple test. The paths rejoin after the switch completes.
Branches in a computer program. The test expression selecting a branch or path in a simple branch or if-statement spans a broad spectrum from simple to arbitrarily complex. The branches typically, but optionally, rejoin at some point in the program. The test expressions selecting the branches in a multi-way branch or switch statement are simple tests for equality.