Statements and Control Flow

A simple C++ statement is one of the individual instructions in a program — such as a variable declaration or an expression. Each simple statement ends with a semicolon (;) and is executed in the order it appears in the code.

int x = 5;
x = x + 2;
cout << x;

In the above example, each line is a simple statement that ends with a semicolon and runs sequentially from top to bottom.

Beyond Linear Execution

Programs are not limited to a linear sequence of statements. In practice, a program might:

  • Repeat certain parts of its code (loops).
  • Make decisions to execute different code paths (conditionals).

To achieve this, C++ provides flow control statements - tools that tell the program what to do, when to do it, and under what conditions.

Examples include:

  • if / else (conditional branching)
  • switch (multi-way branching)
  • while, for, and do-while (loops)

Compound Statements

A compound statement — a group of statements enclosed in curly braces {}. These grouped statements are treated as one single block.

A compound statement looks like this:

{
    statement1;
    statement2;
    statement3;
}

Each substatement inside the block ends with its own semicolon, and the entire block acts as a single statement.