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