C++ break
statement
The break
statement in C++ is used to terminate the execution of a loop or a switch
statement before it naturally completes its execution. It is an important control flow tool that helps you exit loops and switch
cases when a specific condition is met.
In this blog post, we'll cover:
break
statement.for
, while
, do...while
).switch
statements.break
StatementThe syntax of the break
statement is simple:
break;
When encountered, it immediately exits the innermost loop or switch
statement and transfers control to the code following the loop or switch
.
break
Statement in LoopsIn loops (for
, while
, do...while
), the break
statement can be used to terminate the loop before the condition is false
or the loop reaches its natural end.
for
Loop Early#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
cout << "Breaking the loop at i = " << i << endl;
break; // Exit the loop when i is 5
}
cout << "i = " << i << endl;
}
return 0;
}
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Breaking the loop at i = 5
Explanation:
i
is 5.i
equals 5, the break
statement is executed, causing the loop to terminate prematurely.while
Loop Early#include <iostream>
using namespace std;
int main() {
int i = 0;
// While loop to print numbers until 5 is reached
while (i < 10) {
if (i == 5) {
cout << "Breaking the loop at i = " << i << endl;
break; // Exit the loop when i is 5
}
cout << "i = " << i << endl;
i++;
}
return 0;
}
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Breaking the loop at i = 5
Explanation:
while
loop checks the condition i < 10
on every iteration.i
reaches 5, the break
statement is executed, and the loop terminates early.break
Statement in do...while
LoopsThe break
statement can also be used in a do...while
loop, where the loop always executes at least once before checking the condition.
do...while
Loop Early#include <iostream>
using namespace std;
int main() {
int i = 0;
do {
cout << "i = " << i << endl;
if (i == 3) {
cout << "Breaking the loop at i = " << i << endl;
break; // Exit the loop when i is 3
}
i++;
} while (i < 10);
return 0;
}
Output:
i = 0
i = 1
i = 2
i = 3
Breaking the loop at i = 3
Explanation:
do...while
loop runs at least once before the condition is checked.break
statement causes the loop to terminate early when i
reaches 3.break
Statement in a switch
StatementThe break
statement is commonly used in switch
statements to terminate a case and exit the switch
block. Without break
, the code will "fall through" and continue executing the next case even if the condition is not met.
break
in a switch
Statement#include <iostream>
using namespace std;
int main() {
int num = 2;
switch (num) {
case 1:
cout << "Case 1" << endl;
break; // Exit the switch after case 1
case 2:
cout << "Case 2" << endl;
break; // Exit the switch after case 2
case 3:
cout << "Case 3" << endl;
break; // Exit the switch after case 3
default:
cout << "Default case" << endl;
}
return 0;
}
Output:
Case 2
Explanation:
switch
statement checks the value of num
.case 2
, the break
statement is executed to exit the switch
block.break
statement were omitted, it would continue executing the next cases until it finds a break
or reaches the end.break
Sometimes, loops might run infinitely, either due to a logic error or an unpredictable condition. Using the break
statement, you can avoid infinite loops by setting a termination condition.
#include <iostream>
using namespace std;
int main() {
int i = 0;
// Infinite loop with a break statement
while (true) {
cout << "i = " << i << endl;
if (i == 3) {
cout << "Breaking the loop at i = " << i << endl;
break; // Exit the infinite loop when i is 3
}
i++;
}
return 0;
}
Output:
i = 0
i = 1
i = 2
i = 3
Breaking the loop at i = 3
Explanation:
while (true)
condition creates an infinite loop.break
statement is used to terminate the loop when i
reaches 3.break
Statementbreak
statement is useful when you want to exit a loop early based on a specific condition.switch
statement: It is used to prevent fall-through behavior and exit a switch
case once a match is found.