C++ Loop Types

    In this C++ tutorial, we will discuss what is a loop and the different types of looping methods that we can use in C++.

    What is Loop in C++?

    While programming, we could come across situations where we want a block of code to execute multiple times. Well, in every high-level programming language , including C++, we have the concept of loops that allows us to execute a block of code repeatedly. Generally, a loop is a statement that is capable of executing a block of code again and again until the condition of the loop becomes false or there is a break statement.

    Types of Loops in C++

    In C++, there are three different types of loops:

    • for
    • while
    • do…while

    1. for loop

    We use a for loop when we know the exact number of times we want to execute the block of code. While defining a for loop, we should take care of three things - initialization, condition, and increment or decrement. In for loop, for some situations, we can skip the initialization part but each for loop must have the condition and increment or decrement statements. The for loop keeps executing the block of code until the condition becomes false or it counters a break statement. for loop syntax:

    for ( initialization ; condition ; increment or decrement )
        {
          //For loop block of code
        }

    for loop example:

    #include <iostream>
    using namespace std;
    
    int main()
        {
           for(int i=0 ; i <5 ; i++)
              { //block of for loop starts here
                
                 cout<<"This statement will print 5 times";                             //it will print 5 times
                 cout<<endl;                      // to enter a new line
              } //for loop block ends here
        }

    Output

    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times

    Behind the code In the above code, we used the for loop and executed the cout<<"This statement will print 5 times"; statement 5 times. Additionally, in the above for loop example: Initialization was int i = 0; Condition was “execute the for loop block till i<5;” Increment or decrement was i++

    2. while loop

    A while loop is similar to for loop. The while loop comprises a condition , and the block of code keeps on executing till the while loop condition is true. If the loop condition becomes false or it was always false since the beginning of the while loop, the block of code within the loop would not execute. while loop syntax

    while (condition)
           {
             //while loop block
          }

    while loop example:

    #include <iostream>
    using namespace std;
    
    int main()
        {
             int i=0;
             while(i<5) //while loop keep on executing till the value of i less than 5
                 { //block of while loop starts here
                    
                    cout<<"This statement will print 5 times";                             //it will print the value of i
                    cout<<endl;                      // to enter a new line
                    i+=1;                      // this will increase the value of i by one for each iteration.
                  } //while block ends here
       }

    Output

    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times

    Behind the code This while loop example works the same as the above for loop example. Here we also have a condition (i<5) and an increment i+=1; statement. The while loop block keeps executing till the value of i is less than 5. Also, for each iteration of the while loop block, we have the i+=1; statement, which keeps on increasing the value of i by 1. So, after the 5 th iteration, the value of i becomes 5, and the condition (i<5) becomes false. Thus, the while loop will get terminated.

    3. do…while loop

    In the do-while loop, the condition of the loop resides at the end block of the loop structure. So, even if the condition is false, the loop will execute at least one time. do….while syntax:

    do
        {
             // do while block code
        } while (condition);

    do…while example:

    #include <iostream>
    using namespace std;
    
    int main()
       {
          int i=0;
          do
                { //block of do while loop starts here
                   cout<<"This statement will print 5 times";                             //it will print the value of i
                   cout<<endl;                      // to enter a new line
                   i+=1;                      // this will increse the value of i by one for each iteraation.
                 } while(i<5);
    }

    Output

    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times
    This statement will print 5 times

    Loop Control Statements

    The loop control statements are keywords used inside the loop block, and they are capable of altering the normal execution sequence of the loop. In C++ we have 3 basic loop control statements or keywords that are as follows:

    • break
    • continue
    • goto

    1. break

    The break statement terminates the loop and gets us out of the iteration sequence.

    2. continue

    The continue statement skips the remaining body of the loop and gets back to the starting point of the loop block.

    3. goto

    The goto statement jumps to the corresponding labeled statement.

    C++ Loops Summary

    • A loop is a statement that is capable of executing the same block of code multiple times.
    • Loop basically works on the specified condition. If the condition is true, the loop executes. If the condition is false, the loop terminates.
    • In C++, we have 3 statements to create a loop structure, or we can say that there are three types of loops - for loop, while loop, and do...while loop.
    • There are three loop control statements that we can use inside the loop block: i. break can terminate the loop. ii. continue skip the loop body and get back to the block starting point. iii. goto jumps to the labeled statement.

    To Conclude it All

    We hope that this C++ tutorial has helped you understand what is a loop and the different types of loops that you can use to execute a block of code multiple times. Also, while working with C++ loops, it's important to be aware of the loop control statements that we have discussed above. If you have any doubts or queries related to C++ loop types, you can share them with us in the comments below. People are also reading: