C++ For Loop

The for loop in C++ is a control flow statement that allows you to iterate over a sequence of values, such as the elements of an array or a range of numbers. It is a powerful and versatile looping construct that is commonly used in C++ programs.

Syntax

The basic syntax of a for loop in C++ is as follows:

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

The initialization part of the for loop is executed before the loop starts. It is typically used to initialize a loop counter variable. The condition is evaluated before each iteration of the loop. If the condition is true, the loop will continue executing. If the condition is false, the loop will exit. The increment is executed after each iteration of the loop. It is typically used to increment the loop counter variable.

Examples

Simple For Loop

The following example demonstrates a simple for loop that iterates from 1 to 10 and prints the current value of the loop counter variable:

#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        std::cout << i << std::endl;
    }
}

In this example, the loop counter variable i is initialized to 1. The loop will continue executing as long as i is less than or equal to 10. After each iteration, i is incremented by 1. The code inside the loop body simply prints the current value of i.

For Loop with Array

Here’s an example of a for loop that iterates over the elements of an array:

#include <iostream>

int main() {
    int numbers[] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++) {
        std::cout << numbers[i] << std::endl;
    }
}

In this example, the loop counter variable i is initialized to 0. The loop will continue executing as long as i is less than 5. After each iteration, i is incremented by 1. The code inside the loop body simply prints the current element of the numbers array, which is accessed using the [] operator.

For Loop with Range-based

Here’s an example of a C++11 range-based for loop that iterates over the elements of an array:

#include <iostream>

int main() {
int numbers[] = {1, 2, 3, 4, 5};

for (int x : numbers) {
    std::cout << x << std::endl;
}
}

In this example, the range-based for loop iterates over the elements of the numbers array and assigns each element to the variable x. The code inside the loop body simply prints the current value of x.

Nested Loops

It is also possible to use for loops within other for loops. This is known as nested loops. Here’s an example of a nested loop that prints a multiplication table:

#include <iostream>
int main() {
    for (int i = 1; i <= 10; i++) {
        for (int j = 1; j <= 10; j++) {
            std::cout << i * j << "\t";
        }
        std::cout << std::endl;
    }
}

In this example, the outer for loop iterates from 1 to 10, and the inner for loop also iterates from 1 to 10. The code inside the inner loop body calculates the product of the current values of i and j and prints it. The outer loop is used to move to the next line after every inner loop iteration.

Foreach Loop

C++17 introduces the foreach loop which is a simpler and more readable way to iterate over a container. Here’s an example of a foreach loop that iterates over the elements of an array:

#include <iostream>
#include <array>
int main() {
std::array<int, 5> numbers = {1, 2, 3, 4, 5};

for(auto x : numbers) {
    std::cout << x << std::endl;
}
}

In this example, the foreach loop iterates over the elements of the numbers array and assigns each element to the variable x. The code inside the loop body simply prints the current value of x.

In conclusion, the for loop in C++ is a powerful and versatile looping construct that can be used to iterate over a sequence of values, such as the elements of an array or a range of numbers. It can also be nested, and with C++17, it can be used with the foreach loop for simpler and more readable code when iterating over containers.

Leave a Reply

Your email address will not be published. Required fields are marked *