How to Terminate a Loop in C++?

In this tutorial, we are going to learn about the topic of termination of the loop in c++. For the execution of a block of statements repeatedly we use the loop. What is termination in the loop? Terminate means ending something forcefully. In this context, Termination means that we can exit the loop at our will.

Types of Loops

There are 3 types of loops in C++.

  1. for loop:
    Syntax:

    for(initialization; condition ; increment/decrement)
    {
       C++ statement(s);
    }

    Firstly the loop variable is initialize, this step occurs only once. Then condition is check if it is true then the code block inside the loop is executes. Else the program exit from the loop. Once the statements inside loop execute. Then control jumps to increment/decrement part. Finally the condition is check again and the whole process repeats until the condition is false.

  2. while loop:
    Syntax:

    while(condition)
    {
       statement(s);
    }

    In while loop, condition is check first and if it is true then the statements inside while loop executes, this happens repeatedly until the condition is false. When the condition returns false, the control comes out of our loop.

  3. do-while loop:
    Syntax:

    do
    {
       statement(s);
    } while(condition);

    This loop is different than the previous loops. First, the statements inside the loop execute then the condition is check. If the condition is false then program exit from loop. Else the statements inside loop executes. In short, it means that the statements inside the loop are executed at least once.

Ways to terminate a loop in C++

There are two ways we can follow to terminate a loop in c++. First one is by the usage of break keyword. Second by the use of exit() function.

 




Using break keyword

We use break keyword to terminate the loop. Once it executes the control jumps from the loop to the next statement after the loop in the program.

Example:

int i=1;
while(i < 10){
    if(i == 5)
    break;
    cout<<i<<endl;
}
cout<<"End";

Output:

1
2
3
4
End

Explanation:

In the above code, an example of break statement is shown. Variable i is initialized with¬† value 1. Loops condition is to run until (i < 10). There is another condition inside the loop (i==5) which if holds true executes the break statement. The numbers are printed until 4 then the condition for break statement holds true. Once break executes the control jumps out of the loop to the next statement in the program. And prints “End”.

Using exit() function

In c++ exit() is a predefined function. When exit() is encountered it forces the whole program to exit.

Example:

int i=1;
while(i<10){
    if(i == 5)
    exit(-1);
    cout<<i<<endl;
}
cout<<"Endl";

Output:

1
2
3
4

Explanation:

The above example shows the working of exit() function. All the conditions and statements are same as the previous example. But still, the output is different. It is due to different way of working of exit() function and break statement. When exit() statement executes it jumps the control to outside of program and any statement after that does not executes.

 

Also Checkout:

Types of loops

C++ program to create a text file, open and read a particular line


Leave a Reply

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