C++

C++ Example Of A For Loop – Q&A

When a condition is not met, a loop is used to repeatedly execute a block of statements. For instance, you might wish to set a variable’s value to 1 and show a number from 1 to 100 times, increasing its value by 1 with each loop iteration.

For, while, and do-while are the three main types of loops available in C++. We will learn how to use the “for loop” in C++ in this tutorial.

Format of the for loop

for(initialization; condition ; increment/decrement)
{
   C++ statement(s);
}

Flow of the for loop’s execution

The interpreter constantly maintains track of which statement is scheduled to be executed when a program runs. This is referred to as the control flow or the program execution flow.

First step: The initialization phase of the for loop only executes once because initialization occurs first and only once in the for loop.

Second step: Every time the for loop iterates, the condition is checked to see if it is true. If it is, the statements inside the body of the for loop are then performed. Once the condition returns false, the statements in the for loop are not executed, and control is instead passed to the statement that follows the for loop in the program.

Third step: The increment/decrement portion of the for loop, which updates the loop counter, runs after each execution of the for loop’s body.

Fourth step: The control moves directly to step two and reevaluates the condition after the third step.

Up until the loop condition returns false, the second through fourth stages are repeated.

Simple For loop in C++ example

Here, I initialized the loop with the variable I set to 1, with the condition being that i<=6. The value of I increases by 1 with each loop iteration.

#include <iostream>
using namespace std;
int main(){
   for(int i=1; i<=6; i++){
      /* This statement would be executed
       * repeatedly until the condition
       * i<=6 returns false.
       */
      cout<<"Value of variable i is: "<<i<<endl;
   }
   return 0;
}

Output:

Value of variable i is: 1
Value of variable i is: 2
Value of variable i is: 3
Value of variable i is: 4
Value of variable i is: 5
Value of variable i is: 6

Infinite for loop in C++

When a loop continues to run indefinitely without stopping, it is said to be endless. Usually, this happens unintentionally. For loops become infinite loops when the condition is configured so that it never returns false.

For example:

#include <iostream>
using namespace std;
int main(){
   for(int i=1; i>=1; i++){
      cout<<"Value of variable i is: "<<i<<endl;
   }
   return 0;
}

Since we continue increasing the value of I the condition i>=1 would always be satisfied, never returning false, resulting in an eternal loop.

Another instance of an infinite for loop is as follows:

// infinite loop
for ( ; ; ) {
   // statement(s)
}

Example: Using a for loop, display array elements

#include <iostream>
using namespace std;
int main(){
   int arr[]={21,9,56,99, 202};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   for(int i=0; i<5; i++){
      cout<<arr[i]<<endl;
   }
   return 0;
}

Output:

21
9
56
99
202

Related Articles

Leave a Reply

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

Back to top button