in , ,

C++ for Loop

C++ for Loop
C++ for Loop

C++ for Loop: In this tutorial, we will find out about the C++ for loop, and it’s working with the help of certain examples.

in computer programming, loops are used to rehash a square of code.

For instance, suppose we need to show a message multiple times. At that point as opposed to composing the print articulation multiple times, we can use a loop.

That was only a basic model; we can accomplish significantly more productivity and refinement in our programs by using loops.

There are 3 sorts of circles in C++.

for loop
while loop
do…while loop
This tutorial focuses on C++ for loop. We will learn about the other type of loops in the upcoming tutorials.


C++ for loop

The syntax of for-loop is:

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

Here,

initialization – initializes variables and is executed only once
condition – if true, the body of for loop is executed
if false, the for loop is terminated
update – updates the value of initialized variables and again checks the condition


Example 1: Printing Numbers From 1 to 5

#include <iostream>

using namespace std;

int main() {
        for (int i = 1; i <= 5; ++i) {
        cout << i << " ";
    }
    return 0;
}

Output

1 2 3 4 5

Here is how this program works

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 5 times

// C++ Program to display a text 5 times

#include <iostream>

using namespace std;

int main() {
    for (int i = 1; i <= 5; ++i) {
        cout <<  "Hello World! " << endl;
    }
    return 0;
}

Output

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Here is how this program works

IterationVariablei <= 5Action
1sti = 1trueHello World! is printed and i is increased to 2.
2ndi = 2trueHello World! is printed and i is increased to 3.
3rdi = 3trueHello World! is printed and i is increased to 4.
4thi = 4trueHello World! is printed and i is increased to 5.
5thi = 5trueHello World! is printed and i is increased to 6.
6thi = 6falseThe loop is terminated

Example 3: Find the sum of first n Natural Numbers

// C++ program to find the sum of first n natural numbers
// positive integers such as 1,2,3,...n are known as natural numbers

#include <iostream>

using namespace std;

int main() {
    int num, sum;
    sum = 0;

    cout << "Enter a positive integer: ";
    cin >> num;

    for (int count = 1; count <= num; ++count) {
        sum += count;
    }

    cout << "Sum = " << sum << endl;

    return 0;
}

Output

Enter a positive integer: 10
Sum = 55

In the above example, we have two variables num and sum. The sum variable is doled out with 0 and the num variable is doled out with the worth gave by the user.

Note that we have used a for loop.

for(int count = 1; count <= num; ++count)

Here,

int count = 1: initializes the count variable
count <= num: runs the loop as long as count is less than or equal to num
++count: increase the count variable by 1 in each iteration
When count becomes 11, the condition is false and sum will be equal to 0 + 1 + 2 + … + 10.


Ranged Based for Loop

In C++11, another range-based for loop was acquainted with work with assortments, for example, arrays and vectors. Its syntax is:

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

Here, for every value in the collection, the for loop is executed and the value is assigned to the variable.


Example 4: 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

In the above program, we have proclaimed and introduced an int cluster named num_array. It has 10 things.

Here, we have used a range-based for loop to access to all the things in the array.


C++ Infinite for loop

If the condition in a for loop is always true, it runs forever (until memory is full). For example,

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

In the above program, the condition is always true which will then run the code for infinite times.

In the next tutorial, we will learn about while and do…while loop.


Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

How Adaptive Battery in Android Makes Your Phone Battery Last Longer

How Adaptive Battery in Android Makes Your Phone Battery Last Longer

How to Enable Developer Options in Android

How to Enable Developer Options in Android