Understanding Loops in C++

  • Last updated Apr 26, 2024

Loops in C++ are used to perform a specific set of instructions repeatedly. They are particularly helpful when you need to iterate over arrays, collections, or perform a task a fixed number of times.

C++ offers three main types of loops:

  1. for loop:
  2. The for loop is widely used when you know in advance how many times you want to execute a block of code. It consists of an initialization, a condition, and an increment/decrement statement.

    Here is the basic structure of a for loop in C++:

    for (initialization; condition; update) {
        // Code to be executed repeatedly
    }

    Here's what each part of the for loop does:

    • initialization: This is used to initialize variables and it is executed only once.
    • condition: This is a boolean expression that is evaluated before each iteration. If the condition is true, the loop continues to run; if it's false, the loop terminates.
    • update: The update expression increments or decrements the value of the loop variable in the update expression on every loop.
    • The code inside the curly braces {} is the body of the loop. It's the code that is executed repeatedly as long as the condition is true.

    Here's an example of a simple for loop that prints numbers from 1 to 10:

    #include <iostream>
    
    int main() {
        for (int i = 1; i <= 10; i++) {
            std::cout << i << " ";
        }
        std::cout << std::endl;
        return 0;
    }

    In this example, the for loop initializes int i to 1, checks if i is less than or equal to 10, and after each iteration, increments i by 1. The loop continues until i is no longer less than or equal to 10, and it prints the values of i in each iteration. The output will be 1 2 3 4 5 7 8 9 10.

  3. while loop:
  4. The while loop is used when you want to execute a block of code as long as a condition remains true. It is more flexible than the for loop and can be helpful when you don't know the exact number of iterations in advance.

    Here is the basic structure of a while loop in C++:

    while (condition) {
        // Code to be executed repeatedly
    }

    Here's what each part of the while loop does:

    • condition: This is a boolean expression that is evaluated before each iteration. If the condition is true, the loop continues to run; if it's false, the loop terminates.
    • The code inside the curly braces {} is the body of the loop. It's the code that is executed repeatedly as long as the condition is true.

    Here's an example of a simple while loop that prints numbers from 1 to 10:

    #include <iostream>
    
    int main() {
        int i = 1;
        while (i <= 10) {
            std::cout << i << " ";
            i++;
        }
        std::cout << std::endl;
        return 0;
    }

    In this example, the while loop checks if i is less than or equal to 10. As long as this condition is true, the loop continues, and it prints the values of i in each iteration. The variable i is incremented within the loop to avoid an infinite loop. The output will be 1 2 3 4 5 6 7 8 9 10.

  5. do-while loop:
  6. Do-while loop is an exit controlled loop meaning the condition is tested at the end of the loop body. Hence, it guarantees that the loop body executes at least once, regardless of the condition, whether it is true or false, as the condition is checked after the first execution.

    Here is the basic structure of a do-while loop in C++:

    do {
        // Code to be executed at least once
    } while (condition);

    Here's what each part of the do-while loop does:

    • The code inside the curly braces {} is the body of the loop. It's the code that is executed at least once, regardless of the initial condition.
    • condition: This is a boolean expression that is evaluated after each execution of the loop body. If the condition is true, the loop continues to run; if it's false, the loop terminates.

    Here's an example of a simple do-while loop that prints numbers from 1 to 10:

    #include <iostream>
    
    int main() {
        int i = 1;
        do {
            std::cout << i << " ";
            i++;
        } while (i <= 10);
        std::cout << std::endl;
        return 0;
    }

    In this example, the do-while loop prints the value of i in each iteration and increments i. Since the condition i <= 10 is checked at the end of the loop, the loop will execute at least once, even if i starts with a value greater than 10. The output will be 1 2 3 4 5 6 7 8 9 10.