Skip to content

Latest commit

 

History

History
105 lines (87 loc) · 1.45 KB

forloop.md

File metadata and controls

105 lines (87 loc) · 1.45 KB
layout title parent nav_order
default
for Loop
Iterative
1

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.

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


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

C++ Nested For Loop Example
#include <iostream>  
using namespace std;  
   
int main () {  
        for(int i=1;i<=3;i++){      
             for(int j=1;j<=3;j++){      
            cout<<i<<" "<<j<<"\n";      
          }     
        }  
    }  

Output

1 1
1 2
1 3
2 1
2 2 
2 3
3 1
3 2
3 3

C++ Infinite For Loop
#include <iostream>  
using namespace std;  
   
int main () {  
        for (; ;)    
          {    
                  cout<<"Infinitive For Loop";    
          }    
    }

Output

Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
ctrl+c