Introduction
The break
statement in C++ is used to exit a loop or switch statement prematurely. It provides a way to terminate the execution of a loop or switch case before it has completed all its iterations or case evaluations. This is particularly useful for controlling the flow of your program when a certain condition is met.
Using break in Loops
Example: Using break
in a for
Loop
In this example, we use the break
statement to exit a for
loop when the loop control variable reaches a specific value.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
cout << "i = " << i << endl;
}
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
Explanation
- The
for
loop initializesi
to 0 and increments it by 1 in each iteration. - When
i
equals 5, thebreak
statement is executed, and the loop is terminated.
Example: Using break
in a while
Loop
In this example, we use the break
statement to exit a while
loop when a certain condition is met.
#include <iostream>
using namespace std;
int main() {
int i = 0; // Initialization of loop control variable
while (i < 10) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
cout << "i = " << i << endl;
i++; // Increment the loop control variable
}
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
Explanation
- The
while
loop continues to execute as long asi
is less than 10. - When
i
equals 5, thebreak
statement is executed, and the loop is terminated.
Example: Using break
in a do-while
Loop
In this example, we use the break
statement to exit a do-while
loop when a certain condition is met.
#include <iostream>
using namespace std;
int main() {
int i = 0; // Initialization of loop control variable
do {
if (i == 5) {
break; // Exit the loop when i equals 5
}
cout << "i = " << i << endl;
i++; // Increment the loop control variable
} while (i < 10); // Condition
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
Explanation
- The
do-while
loop ensures that the loop body is executed at least once. - When
i
equals 5, thebreak
statement is executed, and the loop is terminated.
Using break in switch Statements
Example: Using break
in a switch
Statement
In this example, we use the break
statement to exit a switch
statement after a matching case is executed.
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
default:
cout << "Weekend" << endl;
}
return 0;
}
Output
Wednesday
Explanation
- The
switch
statement evaluates the value ofday
. - When
day
equals 3, thecase 3
block is executed, printing "Wednesday". - The
break
statement exits theswitch
statement, preventing the execution of subsequent cases.
Conclusion
The break
statement is a powerful control flow tool in C++ that allows you to exit loops and switch statements prematurely. It provides greater control over the flow of your program and helps you handle specific conditions more efficiently. This chapter covered the use of the break
statement in for
, while
, and do-while
loops, as well as in switch
statements, with simple examples and their outputs. Understanding how to use the break
statement effectively will help you write more flexible and readable code. In the next chapter, we will explore the continue
statement in C++.