C++Tutorials

C++ for loop

In this article we will learn about C++ for loop, The C++ for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop than while or do-while loops.

The C++ for loop is same as C/C#. We can initialize variable, check condition and increment/decrement value.

C++ for Loop structure

for(initialization; condition; update){    
//code to be executed    
}    

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

C++ for Loop example

#include <iostream>  
using namespace std;  
int main() {  
         for(int i=1;i<=10;i++){      
            cout<<i <<"\n";      
          }       
    }   

Output:

1
2
3
4
5
6
7
8
9
10

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button