What is the break Statement in C++
The break statement is a control flow statement that allows you to exit out of a loop or switch statement before its natural termination. It is used to break the flow of execution when a particular condition is met. When a break statement is encountered, the control is immediately transferred to the first statement after the loop or switch block.
Syntax of the break Statement
The syntax for the break statement in C++ is quite simple, which is given below:
The above syntax is used within the loop or switch statement to terminate the execution of the block.
How Does a break Statement Work in C++?
A break statement is a programming command that tells the computer to stop running a loop or switch statement and move on to the next command. When a break statement appears within a loop, it stops the loop from running and moves on to the next command after the loop. This is useful for quickly exiting a loop when a certain condition is met.
The break statement can also be used with the if-else statement, but it should always be declared within the loop body and only used for one condition at a time.
In switch statements, the break statement is typically used at the end of each case to ensure that the program doesn’t continue to execute subsequent cases. When the break statement is encountered, the program jumps out of the switch statement and moves on to the next command.
The break statement can be used in the following types of loops:
- for loop
- while loop
- do-while loop
- Switch Case
Examples of break Statements in C++ Programs
Let’s look at some examples of break statements in C++ programming language.
Example 1: break Statement with Simple For-Loop
using namespace std;
int main() {
for (int a = 1; a <= 20; a++)
{
if (a == 10)
{
break;
}
cout << a << " ";
}
return 0;
}
The above code defines a main function that uses a for loop to iterate through the integers from 1 to 20. The loop is exited prematurely using the break statement when the loop variable a equals 10. The program then prints the integers from 1 to 9 to the console.
Output
Example 2: break Statement with Switch Case
using namespace std;
int main(){
int a = 3;
switch (a) {
case 1:
cout << "Case 1: Today is Monday" << endl;
break;
case 2:
cout << "Case 2: Today is Tuesday" << endl;
break;
case 3:
cout << "Case 3: Today is Wednesday" << endl;
break;
case 4:
cout << "Case 4: Today is Thursday" << endl;
break;
case 5:
cout << "Case 5: Today is Friday" << endl;
break;
}
return 0;
}
In the above example, when the program executes the switch statement, Case 3 will be executed as “a” is initialized to 3. The break statement helps to terminate the switch statement without executing the other cases subsequently.
Output
Example 3: break Statement with Do-While Loop
using namespace std;
int main() {
int num;
do {
cout << "Enter a positive number (-1 to exit): ";
cin >> num;
if (num == -1) {
break;
}
cout << "You entered: " << num << endl;
} while (num > 0);
cout << "Program exited." << endl;
return 0;
}
The above program prompts the user to enter a positive number, and if the user enters -1, the loop is exited using the break statement. If the user enters a positive number, the program displays the number, and the loop continues until the user enters -1.
Note that the do-while loop in this example ensures that the loop is executed at least once, even if the user enters -1 on the first iteration.
Output
Advantages of break Statement
The break statement is helpful in a program because it allows you to terminate a loop or switch statement prematurely, based on some condition. The primary advantage of using a break statement is that it can save processing time and resources by allowing the program to exit a loop or switch statement as soon as the desired condition is met, rather than continuing to evaluate the condition in subsequent iterations.
Conclusion
In C++, the break statement can make your code more efficient by allowing you to exit a loop or switch statement as soon as the desired condition is met, rather than continuing to evaluate the condition unnecessarily. In this article, we have provided the concept, syntax, working, and some examples of break statements in for-loop, switch case, and do-while.
from https://ift.tt/7vq6r2m
0 Comments