For-loop-in-C++ .

In this session i have discussed for loop in C++ .


The for loop also a entry control loop in C++ .

For Loop Syntax :

    for(initialization ;test-condition;increment/decrement)
    {
        loop body
    }
    

initialization :

In the for loop first initialize the variable like i=1 etc . This is called the control variable .

test-condition :

The next is test the condition . In which the value of variable is evaluated with specified condition . The condition may be relational expression . The loop will continue if condition is true other wise loop will be terminated .

increment/decrement :

In the segment the value of variable will be increment or decrement according to your need .


Example : Write a program that print the value 1-10 using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {  
         for(int i=1;i<=10;i++)
         {
              cout<<i;
              cout<<" ";
         }
         return 0;
    }    
    
   OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

Example : Write a program that print the table of 2 using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
            int t;
            for (int i=1;i<=10;i++)
            {
                t = i * 2;   
                cout<<t;
                cout<<" ";
            }
            return 0;
    }    
    
    OUTPUT : 2 4 6 8 10 12 14 16 18 20
    

Example : Write a program that print the even number using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
           for (int i=1;i<=10 ; i++)
           {
                 if (i % 2 == 0)
                 {
                    cout<<i;
                    cout<<" ";
                 }
           }
           return 0;
    }    
    
    OUTPUT : 2 4 6 8 10
    

Example : Write a program that print the odd number using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
           for (int i=1;i<=10 ; i++)
           {
                 if (i % 2 != 0)
                 {
                    cout<<i;
                    cout<<" ";
                 }
           }
           return 0;
    }    
    
    OUTPUT : 1 3 5 7 9
    

Example : Write a program that print the following triangle using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
           for (int i=1;i<=5 ; i++)
           {
                for (int j = 1; j <= i;j++ )
                {
                    cout<<"*";
                }
                cout<<"\n";
            }
            return 0;
    }    
    
     OUTPUT :   *
                * *
                * * *
                * * * *
                * * * * *
    

Example : Write a program that print the following number triangle using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
           for (int i=1;i<=5 ; i++)
            {
                for (int j = 1; j <= i;j++ )
                {
                    cout<<j;
                }
                cout<<"\n";
            }
            return 0;
    }    
    
     OUTPUT :   1
                1 2
                1 2 3
                1 2 3 4
                1 2 3 4 5
    

Example : Write a program that print the following number triangle using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
           for (int i=1;i<=5 ; i++)
            {
                for (int j = 1; j <= i;j++ )
                {
                    cout<<i;
                }
                cout<<"\n";
            }
            return 0;
    }    
    
     OUTPUT :   1
                2 2
                3 3 3
                4 4 4 4
                5 5 5 5 5
    

Example : Write a program that print the following triangle using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
            int k = 4;
            for (int i=1;i<=5 ; i++)
            {
                for (int space = k; space > 0; space--)
                {
                    cout<<" ";
                }
                    for (int j = 1; j <= i; j++)
                    {
                        cout<<"*";
                    }
                    k--;
                    cout<<"\n";
            }
            return 0;
    }    
    
     OUTPUT :        *
                   * * 
                 * * * 
               * * * * 
             * * * * *
    

Example : Write a program that print the following number triangle using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
            int k = 4;
            int m = 1;
            for (int i=1;i<=4 ; i++)
            {
                for (int space = k; space > 0; space--)
                {
                    cout<<" ";
                }
                for (int j = 1; j <=i; j++)
                {
                    cout<<m;
                    m++;
                }
                 k--;
                 cout<<"\n";
             }
             return 0;
     }    
    
     OUTPUT :          1
                     2 3
                   4 5 6
                 7 8 9 10
    

Example : Write a program that print the following number triangle using for loop .

    #include<iostream>
    using namespace std;  
    int main()
    {
            int k = 3;
            int m = 1;
            for (int i=1;i<=4 ; i++)
            {
                for (int space = k; space > 0; space--)
                {
                  cout<<" ";
                }
                for (int j = 1; j <=m; j++)
                {
                   cout<<"*";
                }
                k--;
                m = m + 2;
                cout<<"\n";
             }
             return 0;
     }    
    
     OUTPUT :        *
                   * * *
                 * * * * *
               * * * * * * *
    

Welcome to AndroidATPoint.

back image                     next image