Skip to content

Control Structures in C++

Control structures are fundamental building blocks in programming that allow you to control the flow of execution in your programs. This section covers the basic control structures in C++, including conditional statements, loops, and branching statements.

Conditional Statements

Conditional statements allow you to execute different blocks of code based on certain conditions.

if Statement

The if statement executes a block of code if a specified condition is true.

int a = 10;
if (a > 5) {
    std::cout << "a is greater than 5" << std::endl;
}

if-else Statement

The if-else statement executes one block of code if a condition is true, and another block of code if the condition is false.

int a = 10;
if (a > 5) {
    std::cout << "a is greater than 5" << std::endl;
} else {
    std::cout << "a is not greater than 5" << std::endl;
}

else-if Ladder

The else-if ladder allows you to check multiple conditions.

int a = 10;
if (a > 10) {
    std::cout << "a is greater than 10" << std::endl;
} else if (a == 10) {
    std::cout << "a is equal to 10" << std::endl;
} else {
    std::cout << "a is less than 10" << std::endl;
}

switch Statement

The switch statement allows you to execute one block of code out of many based on the value of a variable.

int day = 3;
switch (day) {
    case 1:
        std::cout << "Monday" << std::endl;
        break;
    case 2:
        std::cout << "Tuesday" << std::endl;
        break;
    case 3:
        std::cout << "Wednesday" << std::endl;
        break;
    default:
        std::cout << "Invalid day" << std::endl;
}

Loops

Loops are used to execute a block of code multiple times.

while Loop

The while loop executes a block of code as long as a specified condition is true.

int i = 0;
while (i < 5) {
    std::cout << "i is " << i << std::endl;
    i++;
}

do-while Loop

The do-while loop is similar to the while loop, but it executes the block of code at least once before checking the condition.

int i = 0;
do {
    std::cout << "i is " << i << std::endl;
    i++;
} while (i < 5);

for Loop

The for loop is used to execute a block of code a specific number of times. It is often used when the number of iterations is known beforehand.

for (int i = 0; i < 5; i++) {
    std::cout << "i is " << i << std::endl;
}

Range-based for Loop

The range-based for loop is used to iterate over elements in a container or array.

int numbers[] = {1, 2, 3, 4, 5};
for (int n : numbers) {
    std::cout << "n is " << n << std::endl;
}

Branching Statements

Branching statements allow you to alter the flow of execution in your program.

break Statement

The break statement is used to exit a loop or switch statement.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    std::cout << "i is " << i << std::endl;
}

continue Statement

The continue statement skips the current iteration of a loop and proceeds to the next iteration.

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    std::cout << "i is " << i << std::endl;
}

goto Statement

The goto statement transfers control to a labeled statement. Its use is generally discouraged as it can make the code difficult to read and maintain.

int a = 10;
if (a > 5) {
    goto label;
}
std::cout << "This will not be printed." << std::endl;
label:
std::cout << "Jumped to label." << std::endl;

Next Steps

To continue learning about C++ programming, proceed with the following topics:

  1. Functions: Learn how to define and use functions in C++.
  2. Object-Oriented Programming: Dive into the concepts of classes and objects, inheritance, and polymorphism.
  3. Standard Template Library (STL): Discover the power of the STL for efficient programming.

By mastering these control structures, you will be able to write more complex and efficient C++ programs.

Next