When you write a C++ program, the CPU executes statements starting from the first line of main() and continues in sequence until it reaches the last statement. This ordered sequence of executed statements is called the execution path.

Consider this simple calculator program:

#include <iostream>

int main()
{
    std::cout << "Calculator ready\n";

    double firstNumber{};
    std::cout << "First number: ";
    std::cin >> firstNumber;

    double secondNumber{};
    std::cout << "Second number: ";
    std::cin >> secondNumber;

    double result{ firstNumber + secondNumber };
    std::cout << "Result: " << result << '\n';

    return 0;
}

This program follows a straight-line execution path - it always executes the same statements in the same order, regardless of what values the user enters. Every run of this program follows an identical path through the code.

However, real-world programs need more flexibility. What if the user wants to subtract instead of add? What if they enter invalid input and we need to ask again? What if we want to perform multiple calculations without restarting the program? A straight-line program cannot handle these scenarios because it cannot adapt its behavior at runtime.

C++ provides control flow statements that allow your program to make decisions and change its execution path based on conditions. These statements let you write programs that can respond intelligently to different situations.

When a control flow statement causes execution to jump to a different location in the code, this is called branching.

Control flow categories

Category Purpose C++ Keywords
Conditionals Execute code only when specific conditions are met if, else, switch
Jumps Transfer execution to another location goto, break, continue
Function calls Jump to another location and return function calls, return
Loops Repeat code until a condition changes while, do-while, for, ranged-for
Halts Terminate the program immediately std::exit(), std::abort()
Exceptions Handle errors in a structured way try, throw, catch

This chapter covers all categories except exceptions, which are complex enough to deserve their own chapter later in the course.

Before learning about control flow, you were limited to writing simple sequential programs. With control flow, you can write programs that make decisions, repeat actions, and respond to user input in sophisticated ways. This opens the door to creating programs with real practical value.

Let's begin exploring these powerful tools.

Summary

Execution path: The ordered sequence of statements executed by a program. Simple programs follow a straight-line path, executing the same statements in the same order every time.

Control flow statements: Language features that allow programs to change their execution path based on conditions, enabling programs to adapt their behavior at runtime.

Branching: When a control flow statement causes execution to jump to a different location in the code.

Control flow categories: Conditionals (if, else, switch), Jumps (goto, break, continue), Function calls (call, return), Loops (while, do-while, for, ranged-for), Halts (std::exit, std::abort), and Exceptions (try, throw, catch).

Control flow transforms programs from rigid sequential scripts into flexible, intelligent applications that can respond to different situations and user inputs appropriately.