The break statement is used to immediately stop the execution of a loop or a switch statement. When C++ reaches the break statement, it exits the current loop or switch block and continues execution from the statement after it.
break statement is commonly used
with for, while, do-while loops and
switch statements.
The break statement terminates the nearest enclosing loop
or switch statement immediately.
break;
After break executes, program control moves to the statement
following the loop or switch.
break;
The break statement contains only the keyword break
followed by a semicolon.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
std::cout << i << std::endl;
}
Output:
1
2
3
4
When i becomes 5, the break statement terminates the loop.
Consider the following loop:
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break;
}
std::cout << i << std::endl;
}
The execution is:
i == 6, break executes.int i = 1;
while (i <= 10) {
if (i == 5) {
break;
}
std::cout << i << std::endl;
i++;
}
The loop stops when i becomes 5.
int i = 1;
do {
if (i == 5) {
break;
}
std::cout << i << std::endl;
i++;
} while (i <= 10);
The break statement immediately terminates the do-while loop.
int choice = 2;
switch (choice) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
case 3:
std::cout << "Three";
break;
default:
std::cout << "Invalid choice";
}
In a switch statement, break prevents execution from
falling into the next case.
int target = 7;
for (int i = 1; i <= 10; i++) {
if (i == target) {
std::cout << "Number found";
break;
}
}
Once the target number is found, there is no need to continue the loop.
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
if (numbers[i] == 30) {
std::cout << "30 found";
break;
}
}
The loop stops immediately after finding 30.
int number;
while (true) {
std::cout << "Enter a number: ";
std::cin >> number;
if (number == 0) {
break;
}
std::cout << "You entered: "
<< number
<< std::endl;
}
Entering 0 terminates the loop.
A loop with true as its condition is an infinite loop.
A break statement can provide a controlled exit.
while (true) {
std::cout << "Enter 0 to stop: ";
int number;
std::cin >> number;
if (number == 0) {
break;
}
}
The break statement is commonly placed inside an if statement.
for (int i = 1; i <= 10; i++) {
if (i > 5) {
break;
}
std::cout << i << std::endl;
}
The if statement determines when the loop should stop.
char choice;
while (true) {
std::cout << "Enter y to continue or n to stop: ";
std::cin >> choice;
if (choice == 'n') {
break;
}
}
The loop terminates when the user enters n.
int number = 5;
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break;
}
std::cout << number
<< " x "
<< i
<< " = "
<< number * i
<< std::endl;
}
Only the first five lines of the multiplication table are displayed.
int numbers[] = {5, 10, 15, 20, 25};
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << std::endl;
if (numbers[i] == 15) {
break;
}
}
The array traversal stops after 15 is displayed.
#include <iostream>
#include <string>
int main() {
std::string text = "HELLO";
for (int i = 0; i < text.length(); i++) {
if (text[i] == 'L') {
break;
}
std::cout << text[i];
}
return 0;
}
The loop stops when the first L is found.
When break is used inside nested loops, it terminates only the nearest enclosing loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
break;
}
std::cout << j << " ";
}
std::cout << std::endl;
}
Here, break terminates the inner loop, not the outer loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 2) {
break;
}
std::cout << j << " ";
}
std::cout << "Outer loop: "
<< i << std::endl;
}
The inner loop stops at 2, but the outer loop continues.
int choice;
do {
std::cout << "1. Start" << std::endl;
std::cout << "2. Exit" << std::endl;
std::cin >> choice;
if (choice == 2) {
break;
}
} while (true);
std::cout << "Program ended";
int marks;
while (true) {
std::cout << "Enter marks: ";
std::cin >> marks;
if (marks >= 0 && marks <= 100) {
break;
}
std::cout << "Invalid marks"
<< std::endl;
}
std::cout << "Valid marks entered";
The loop ends only after valid marks are entered.
#include <iostream>
#include <string>
int main() {
std::string password;
for (int attempt = 1; attempt <= 3; attempt++) {
std::cout << "Enter password: ";
std::cin >> password;
if (password == "1234") {
std::cout << "Login successful";
break;
}
std::cout << "Wrong password"
<< std::endl;
}
return 0;
}
Once the correct password is entered, the loop stops.
int numbers[] = {12, 25, 36, 48, 50};
int target = 36;
bool found = false;
for (int i = 0; i < 5; i++) {
if (numbers[i] == target) {
found = true;
break;
}
}
if (found) {
std::cout << "Number found";
}
else {
std::cout << "Number not found";
}
The break statement avoids unnecessary searching after the target is found.
int choice;
while (true) {
std::cout << "1. Add" << std::endl;
std::cout << "2. Exit" << std::endl;
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Add selected";
break;
case 2:
std::cout << "Exit selected";
return 0;
default:
std::cout << "Invalid choice";
}
std::cout << std::endl;
}
Here, the break inside the switch exits the switch case. The
return statement ends the complete program.
| break | continue |
|---|---|
| Terminates the loop. | Skips the current iteration. |
| Execution moves outside the loop. | Loop continues with its next iteration. |
| Used when no more iterations are needed. | Used when the current iteration should be skipped. |
// break
if (i == 5) {
break;
}
// continue
if (i == 5) {
continue;
}
Both break and return can stop a loop, but
they have different purposes.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
}
std::cout << "Program continues";
With break, only the loop is terminated and the program
continues after the loop.
int test() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
return 0;
}
}
return 1;
}
A return exits the current function.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
std::cout << i;
}
#include <iostream>
int main() {
int target;
int numbers[] = {10, 20, 30, 40, 50};
std::cout << "Enter number to search: ";
std::cin >> target;
for (int i = 0; i < 5; i++) {
if (numbers[i] == target) {
std::cout << "Number found";
break;
}
if (i == 4) {
std::cout << "Number not found";
}
}
return 0;
}
The loop stops immediately when the target is found.
#include <iostream>
int main() {
int choice;
while (true) {
std::cout << std::endl;
std::cout << "1. Check Balance" << std::endl;
std::cout << "2. Deposit" << std::endl;
std::cout << "3. Withdraw" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << "Enter choice: ";
std::cin >> choice;
if (choice == 4) {
break;
}
std::cout << "Option selected";
}
std::cout << "ATM session ended";
return 0;
}
| Concept | Meaning |
|---|---|
break; |
Immediately terminates the nearest enclosing loop or switch. |
| for Loop | Can stop the loop before all iterations are completed. |
| while Loop | Can terminate a repeating loop based on a condition. |
| do-while Loop | Can stop the loop before another iteration. |
| switch | Usually prevents execution from continuing into the next case. |
| Nested Loop | break affects only the nearest enclosing loop. |
| Search | Can stop searching once a required item is found. |
if (condition) {
break;
}
Question: What happens when a break statement is executed inside a loop?