C++ for loop – with Examples

Through the use of some examples, we will learn about the C++ for loop and how it functions in this tutorial.

Computer programmers employ loops to continually execute a block of code.

Let’s say, for illustration, that we want to display a message 100 times. Then we can use a loop rather than writing the print statement 100 times.

That was just a simple illustration; by using loops effectively, we can make our programs much more sophisticated and efficient.

C++ offers three different kinds of loops.

  1. for loop
  2. while loop
  3. do…while loop.

This guide’s main emphasis is on the C++ for loop. In the upcoming tutorials, we will learn about the additional types of loops.


C++ for loop

The syntax of for-loop is:

for (initialization; condition; update) {
    // body of-loop 
}

Here,

  • initialization: variable initialization; only once execution.
  • condition: If true, the for loop’s body is run. The for loop ends if the condition is false.
  • update: changes the initialised variables’ values, then rechecks the condition.
Flowchart of for loop in C++

Example 1: Printing Numbers From 1 to 5

#include <iostream>
//geektocode.com
using namespace std;

int main() {
        for (int i = 1; i <= 5; ++i) {
        cout << i << " ";
    }
    return 0;
}
OUTPUT:
1 2 3 4 5

The following describes how this programme operates:

IterationVariablei <= 5Action
1sti=1true1 is printed. i is increased to 2.
2ndi=2true2 is printed. i is increased to 3.
3rdi=3true3 is printed. i is increased to 4.
4thi=4true4 is printed. i is increased to 5.
5thi=5true5 is printed. i is increased to 6.
6thi=6falseThe loop is terminated.

Example 2: Display a text 10 times

// C++ Program to display a text 10 times
#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 10; ++i) {
        cout << "GeektoCode! " << endl;
    }
    return 0;
}
OUTPUT:
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!

Example 3: Find the sum of first n Natural Numbers

// C++ program to find the sum of first n natural numbers
//geektocode.com
#include <iostream>
using namespace std;
int main() {
    int num, sum;
    sum = 0;
    cout << "Enter a positive integer: ";
    cin >> num;
    for (int i = 1; i <= num; ++i) {
        sum += i;
    }
    cout << "Sum = " << sum << endl;
    return 0;
}
OUTPUT:
Enter a positive integer: 10
Sum = 55

Ranged Based for Loop

A new range-based for loop was added in C++11 to support working with collections like arrays and vectors. The syntax is:

for (variable : collection) {
    // body of loop
}

In this case, the for loop is executed for each item in the collection, and the value is then assigned to the variable.

Example 1: Range Based for Loop

#include <iostream>
using namespace std;
int main() {
    int num_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int n : num_array) {
        cout << n << " ";
    }
    return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10

An int array named the num array has been declared and initialized in the program above. Ten items make up it.

Here, a range-based for loop is being utilized to access every element of the array.


C++ Infinite for loop

// infinite for loop
for(int i = 1; i > 0; i++) {
    // block of code
}

Because the condition in the programme above is always true, the code will run indefinitely.


We will learn about the while and do…while loop in the upcoming tutorial.


Leave a Reply

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