Skip to content
Related Articles

Related Articles

Improve Article

Exit a loop in C++

  • Difficulty Level : Medium
  • Last Updated : 20 Jan, 2021
Geek Week

Exit a Loop in C++: If the condition of an iteration statement (for, while, or do-while statement) is omitted, that loop will not terminate unless the user explicitly exits it by a break, continue, goto, or some less obvious way such as a call of exit() in C++.

Some common ways to exit a loop are as follows:

Break: This statement is a loop control statement used to terminate the loop. Below is the C++ program to illustrate the use of the break statement:

C++




// C++ program to illustrate the use
// of the break statement
#include <iostream>
using namespace std;
  
// Function to illustrate the use
// of break statement
void useOfBreak()
{
    for (int i = 0; i < 40; i++) {
  
        cout << "Value of i: "
             << i << endl;
  
        // If the value of i is
        // equal to 2 terminate
        // the loop
        if (i == 2) {
            break;
        }
    }
}
  
// Driver Code
int main()
{
  
    // Function Call
    useOfBreak();
  
    return 0;
}
Output:
Value of i: 0
Value of i: 1
Value of i: 2

Explanation: In the above code, the loop terminates after i=2 and prints the values of i before 2 i.e. from 0 to 2.

Continue: The continue statement is used to get to the end of the loop body rather than exiting the loop completely. It skips the rest of the body of an iteration-statement. The main difference between break and continue is that, the break statement entirely terminates the loop, but the continue statement only terminates the current iteration.



Below is the C++ program to illustrate the use of the continue statement:

C++




// C++ program to illustrate the
// use of the continue statement
  
#include <iostream>
using namespace std;
  
// Function to illustrate the use
// of continue statement
void useOfContinue()
{
    for (int i = 0; i < 5; i++) {
  
        // If the value of i is the
        // same as 2 it will terminate
        // only the current iteration
        if (i == 2) {
            continue;
        }
  
        cout << "The Value of i: "
             << i << endl;
    }
}
  
// Driver Code
int main()
{
    // Function Call
    useOfContinue();
  
    return 0;
}
Output:
The Value of i: 0
The Value of i: 1
The Value of i: 3
The Value of i: 4

Explanation: In the above code, the loop terminates the iteration for i=2 and prints the values of i before and after 2. Hence, it only terminates the given iteration rather than the loop.

goto: This statement is an unconditional jump statement used for transferring the control of a program. It allows the program’s execution flow to jump to a specified location within the function. The only restriction is that you cannot jump past an initialize or into an exception handler.

Below is the C++ program to illustrate the use of the goto statement:

C++




// C++ program to illustrate the use
// of goto statement
#include <iostream>
using namespace std;
  
// Function to illustrate the use
// of goto statement
void useOfGoto()
{
    // Local variable declaration
    int i = 1;
  
// Do-while loop execution
LOOP:
    do {
        if (i == 2) {
  
            // Skips the iteration
            i = i + 1;
  
            goto LOOP;
        }
  
        cout << "value of i: "
             << i << endl;
        i = i + 1;
  
    } while (i < 5);
}
  
// Driver Code
int main()
{
    // Function call
    useOfGoto();
  
    return 0;
}
Output:
value of i: 1
value of i: 3
value of i: 4

Explanation: In the above code, it jumps to the given location i.e., LOOP, within the function.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :