C++ for Loop

Hi everyone, inside this article we will see about C++ for loop Statement.

The for loop statement is a control structure in C++ which is used to execute a block of code repeatedly for a specified number of times.

Syntax & Explanation

It is defined using the following syntax:

for (initialization; condition; iteration) {
   statement(s);
}

initialization: This is executed only once before the loop starts. It is used to initialize the loop variable.

condition: This is evaluated before each iteration of the loop. If the condition is true, the loop continues to execute. If it’s false, the loop stops executing.

iteration: This is executed after each iteration of the loop. It is used to update the loop variable.

statement(s): This is the body of the loop, executed repeatedly as long as the condition is true.

Here, a pictorial representation of for loop statement:

A Basic Program

Here’s a simple program in C++ that prints the numbers 1 to 10 using a for loop:

#include <iostream>
using namespace std;

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

   return 0;
}

The output of this program will be:

1 2 3 4 5 6 7 8 9 10

C++ Nested for Loop

A nested for loop in C++ is a for loop that is inside another for loop. The inner for loop is executed completely for each iteration of the outer for loop. This allows you to perform a task multiple times, nested inside another task that is also being repeated.

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

for (initialization1; condition1; iteration1) {
   for (initialization2; condition2; iteration2) {
      statement(s);
   }
}

initialization1 and condition1 define the looping behavior of the outer for loop.

initialization2 and condition2 define the looping behavior of the inner for loop.

statement(s) is the body of the inner loop, executed repeatedly as long as condition2 is true.

Example

Here’s a simple program in C++ that demonstrates the use of a nested for loop:

#include <iostream>
using namespace std;

int main() {
   for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
         cout << i * j << " ";
      }
      cout << endl;
   }

   return 0;
}

The output of this program will be:

1 2 3
2 4 6
3 6 9

In this program, the outer for loop iterates three times (from 1 to 3) and the inner for loop iterates three times for each iteration of the outer loop. The product of i and j is printed to the screen, resulting in a 3×3 multiplication table.

C++ Infinite for Loop

An infinite for loop in C++ is a for loop that never terminates, it runs forever. It is created by having a condition in the for loop that is always true.

Here is an example of an infinite for loop in C++:

for (int i = 1;; i++) {
   statement(s);
}

The body of the loop (statement(s)) will be executed repeatedly until the program is stopped manually or a break statement is used to exit the loop.

It’s important to use infinite loops with caution, as they can lead to an unresponsive program if not used correctly. It’s always better to use a loop that has a well-defined ending condition.

We hope this article helped you to understand about C++ for loop in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.