Loops in C++ | For loop in c++ | while loop in c++ | do while in C++ | Loops in CPP

Loops in C++ 

        There are many problems where you have to perform a particular task again and again for n times. Now writing the code n times is not possible because n can also change. Hence to do such part of the program we use loops. In loops, the code is written once, and then it is performed for n times in a loop.



There are 3 types of looping statement

1. For

2. While

3. Do-while



1. For loop:


Syntax: 

for (initialization; condition; increment or decrement)

{

Block of statement;

}


        A for loop is an entry control loop. Entry control means it checks the condition first and if the condition is satisfied then only the loop is executed otherwise, the next statement is executed.


        In the syntax, you can see the initialization part. This part is executed only once and at the start of the for loop. Then the condition part is executed. This part is checked before starting each cycle of the loop. Whenever the condition is false the loop will be terminated and the next statement is executed. 


        At last the increment or decrement part. This part is executed at the end of every cycle of the loop. It will increase or decrease the value of the variable which was initialized.


        Let us see a program for better understanding.



 A program to understand the for loop 

/* This program is used to print the whole numbers up to n*/


#include<bits/stdc++.h>

using namespace std;


int main()

{

    int n,i;

    cin>>n;

    for(i=0;i<=n;i++)         

    {

        cout<< i<<endl;

    }

    return 0;

}


Input: 5

Output:

0

1

2

3

4

5


        In initialize section ‘i’ is initialized with the value 0 as whole numbers start with zero then the condition is check weather i is greater than n here input n is taken as 5 hence the condition i <= n is TRUE so the loop is executed. 


        At the end of the loop the increment section is executed hence now i value becomes 1 then again the condition is checked, this process is done till i = 5 when i =5 the condition is satisfied as then the loop is executed and then the value of i is incremented to 6 now the condition i<=n becomes FALSE and the execution of the loop is stop



Previous Posts : 



2. While loop:


Syntax:

Initialization; 

while(condition)

{

Block of statement;

Increment or Decrement;

}


        While loop is also an entry control loop. It is similar to the for loop. Here as you can see in the syntax the initialization is done before the loop. Then as soon as the while loop is executed it checks the condition. If the condition is true then the loop is executed. In the end, the increment or decrement is done. Again at the start of the first cycle, the condition is checked.


        Let us see the same program we saw in for-loop but this time we will use while-loop


 A program to understand the While loop 

/* This program is used to print the whole numbers upto n*/


#include<bits/stdc++.h>

using namespace std;


int main()

{

    int n,i;

    cin>>n;

    i=0;                                            // initialization

    while(i<=n)                              //condition check

    {

        cout<<i<<endl;  

        i++;                                    // increment 

    }

    return 0;

}


Input: 5

Output: 

0

1

2

3

4

5


You can see in the above code the working of the while loop is pretty much similar to the for loop



3.    Do-while loop:


Syntax:

Initialization;

do

{

Block of statement;

Increment or Decrement;

} while(condition);


        The do-while loop is the exit control loop. This means, unlike the other two loops the condition is checked after the execution of the loop. Some time in a program you need to execute the loop at least once at that time do-while loop is used.


        The working of the Do-while loop is similar to the While loop the only difference is instead of checking the condition at the start of every cycle do-while loop checks the condition at the end of each cycle.


        Let us see the same program using the do-while loop


 A program to understand the do-while loop 


/* This program is used to print the whole numbers upto n*/


#include<bits/stdc++.h>

using namespace std;


int main()

{

    int n,i;

    cin>>n;

    i=0;                                        // initialization

    do

    {

        cout<<i<<endl;  

        i++;                                    // increment 

    }while(i<=n);                        //condition check

    return 0;

}


Input: 5

Output: 

0

1

2

3

4

5


Input: -1

Output: 0


        Here in the first case when input is 5 the output is similar to the output of the program of the other two loops. But in 2nd case even though the output is -1 the loop is executed once. You can check the other 2 programs with input -1 the output will be nothing



4.    Nested Loops :

 

Syntax: 


for (initialization; condition; increment or decrement)                    //1st loop
{

for (initialization; condition; increment or decrement)            //2nd loop
    {

Block of statement;

    }

}


        Like the Nested if, we can use a loop within another loop. The nested loops are used especially in the case of multidimensional arrays, to read, print, and traverse through each element. Also in pattern programs nested loops are used. The below is the syntax for nested loops. 

Note: There is no such compulsion to use the same loop while defining nested loops. You can use while in for, do while in for, for in for, and vice-versa

A program to understand the Nested loops

 

/* This Program Prints the below pattern 

    1 2 3 4 5 
    1 2 3 4 5 
    1 2 3 4 5 
    1 2 3 4 5 
    1 2 3 4 5    */


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    
    for(int i=1;i<=n;i++)                                //1st loop holds count
    {

        for(int j=1;j<=n;j++)                        //2nd loop to print values
        {
            cout<<j<<" ";        
        }

        cout<<endl;                                    //to print next output in new line
    }
    
    return 0;
}


Input:     5

Output: 

    1 2 3 4 5 
    1 2 3 4 5 
    1 2 3 4 5 
    1 2 3 4 5 
    1 2 3 4 5

       

You would love these Hackerrank solutions (See Here !)  



Post a Comment

0 Comments