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.
Loops in C++ - Part 2 (Exit Controlled Loop)
Similar Reads
exit(1) in C++ In C++, the exit function allows the users to terminate the execution of a program immediately and return the control to the operating system. In this article, we will learn about exit(1) in C++. What does exit(1) mean in a C++ Program?The exit() function defined in the <cstdlib> header in C++
3 min read
How to Throw an Exception in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are objects that represent an error that occurs during the execution of a program. In this article, we will learn how to throw an exception in C++. Throw a C++ ExceptionThrowing an exception means sendin
2 min read
How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements:
2 min read
How to Detach a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to detach a thread in C++. What does Detaching a Thread mean?Detaching a thread means allowing the thread to
2 min read
How to Catch All Exceptions in C++? In C++, exceptions are objects that indicate you have an error in your program. They are handled by the try-catch block in C++. In this article, we will learn how to catch all the exceptions in C++. Catching All Exceptions in C++To catch all kinds of exceptions in our catch block in C++, we can defi
2 min read
How to Open and Close a File in C++? In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fst
2 min read