Control Flow in C++

Introduction

Control flow is a fundamental aspect of programming languages, providing the ability to dictate the order in which statements are executed in a program. In C++, a powerful and versatile language, control flow mechanisms are diverse and cater to various programming needs. In this unit, we will explore the different control flow structures in C++ and understand how they shape the flow of execution within a program.

Control flow refers to the order in which individual statements or instructions in a program are executed.

In programming, when a program runs, it usually follows a straight-line path, executing statements in sequential order from the top of main() to the end.

Consider the following program:

#include <iostream>

int main()
{
    std::cout << "Enter an integer: ";

    int x{};
    std::cin >> x;

    std::cout << "You entered " << x << '\n';

    return 0;
}

The execution path of this program includes lines 5, 6, 8, 10, and 12, in that order. This is an example of straight-line program. Straight-line programs take the same path (execute the same statements in the same order) every time they are run.

However, often this is not what we desire. For example, if we ask the user for input, and the user enters something invalid, ideally we would like to ask the user to make another choice. This is not possible in a straight-line program. In fact, the user may repeatedly enter invalid input, so the number of times we might need to ask hem to make another selection isn't knowable until runtime.

Sequential Execution: The Foundation of Control Flow

Sequential execution is the most straightforward form of control flow, where statements are executed in the order they appear in the code. In C++, statements are generally executed one after another, following the flow of the code from top to bottom.

int main() {
    // Sequential execution
    int x = 5;
    int y = 10;
    int sum = x + y;
    return 0;
}

Conditional Statements: Making Decisions

Conditional statements allow the program to make decisions based on certain conditions. The if, else if, and else statements are commonly used for branching execution paths.

int main() {
    int x = 5;
    
    // Conditional statement
    if (x > 0) {
        // Executed if x is greater than 0
        // ...
    } else {
        // Executed if x is not greater than 0
        // ...
    }
    
    return 0;
}

Switch Statements: Multi-Choice Decision Making

The switch statement provides a way to perform multi-choice decision making based on the value of an expression.

int main() {
    int choice = 2;
    
    // Switch statement
    switch (choice) {
        case 1:
            // Code for case 1
            break;
        case 2:
            // Code for case 2
            break;
        default:
            // Code for other cases
            break;
    }
    
    return 0;
}

Loops: Iterative Control Flow

Loops are used for repetitive execution of a block of code. C++ provides various loops structures, including for, while and do-while.

int main() {
    // For loop
    for (int i = 0; i < 5; ++i) {
        // Code to be repeated
        // ...
    }
    
    // While loop
    int j = 0;
    while (j < 5) {
        // Code to be repeated
        // ...
        ++j;
    }
    
    // Do-while loop
    int k = 0;
    do {
        // Code to be repeated
        // ...
        ++k;
    } while (k < 5);
    
    return 0;
}

Jump Statements: Altering Control Flow

C++ provides jump statements like break, continue, and return to alter the normal control flow. break is used to exit loops, continue skip the rest of the loop and goes to the next iteration, and return is used to exit a function.

 int main() {
    // Break statement
    for (int i = 0; i < 10; ++i) {
        if (i == 5) {
            break; // exit the loop when i is 5
        }
        // Code inside the loop
    }
    
    // Continue statement
    for (int i = 0; i < 10; ++i) {
        if (i == 5) {
            continue; // skip the rest of the loop and move to the next iteration when i is 5
        }
        // Code inside the loop
    }
    
    // Return statement
    return 0; // exit the main function
}