Back to: C Tutorials For Beginners and Professionals
Break Statement in C Language with Examples
In this article, I will discuss the Break Statement in C Language with Examples. Please read our previous articles discussing Jump Statements in C.
Break Statement in C Language:
The break statement in C language is a control flow statement used to terminate the loop and switch statement. When a break statement is encountered within a loop (like for, while, or do-while), the loop is immediately terminated, and the program control resumes at the next statement following the loop. In the context of a switch statement, it terminates a case and exits the switch block.
The break is a keyword. By using break, we can terminate the loop body or switch body. Using the break statement is always optional, but it should be placed within the loop body and switch body only.
Syntax: break;
Flowchart of Break Statement in C:
When it encounters the break statement inside a loop body or switch body, then immediately it terminates the loop and switch execution and executes the statements which are present after the loop body or switch body. But suppose the break statement is not executed. In that case, the statements present after the break statement will be executed, and then it will continue its execution with the next iteration of the loop.
Flow of Break Statement in C
The flow of the break statement in C programming can be understood by looking at how it affects the control flow in loops and switch statements. Here’s a breakdown:
In Loops (for, while, do-while)
- Initialization: The loop starts with its initialization (if any), like setting a counter variable in a for loop.
- Condition Check: The loop’s condition is evaluated. If it’s false, the loop terminates. If true, the loop’s body is executed.
- Execution of Loop Body: The statements within the loop are executed sequentially.
- Encounter break: If a break statement is encountered during the execution of the loop body, the control flow immediately exits the loop.
- Skipping Remaining Iterations: Any statements after the break inside the loop are skipped, and so are the remaining iterations.
- Resuming After Loop: Control resumes from the statement immediately following the loop.
In switch Statements
- Evaluation of switch Expression: The expression in the switch statement is evaluated.
- Matching case: The program control jumps to the case that matches the evaluated expression. If no match is found and a default case is defined, control goes to default.
- Execution of case Block: The statements in the matching case block are executed.
- Encounter break: If a break statement is encountered, the control flow immediately exits the switch block.
- Preventing Fall-through: Without the break, the control would “fall through” to subsequent case blocks (if any), executing their statements regardless of the case match.
- Resuming After switch: After exiting the switch block (either via break or if no break is present, at the end of the switch), control resumes at the statement following the switch.
Using break in a for loop:
In this example, the loop will terminate when the variable i reaches 5.
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { printf("%d ", i); if (i == 5) { break; // Exits the loop when i is 5 } } return 0; }
Output: 0 1 2 3 4 5
Using break in a while loop:
Here, the loop will stop when i becomes 5.
#include <stdio.h> int main() { int i = 0; while (i < 10) { printf("%d ", i); if (i == 5) { break; // Exits the loop when i is 5 } i++; } return 0; }
Output: 0 1 2 3 4 5
Using break in a do-while loop:
In this example, the loop continues until i equals 5.
#include <stdio.h> int main() { int i = 0; do { printf("%d ", i); if (i == 5) { break; // Exits the loop when i is 5 } i++; } while (i < 10); return 0; }
Output: 0 1 2 3 4 5
In each of these examples, the break statement is used to exit the loop prematurely when a certain condition is met. It’s a useful way to stop a loop based on dynamic conditions encountered during its execution.
The break statement within a case in a switch statement in C is essential to prevent the execution from falling through to subsequent cases. Here are some examples to illustrate this:
Example: Simple Switch-Case with Break
#include <stdio.h> int main() { int number = 2; switch (number) { case 1: printf("Number is 1\n"); break; case 2: printf("Number is 2\n"); break; case 3: printf("Number is 3\n"); break; default: printf("Number is not 1, 2, or 3\n"); } return 0; }
In this example, the output will be “Number is 2” as the break statement in case 2 prevents the code from executing the subsequent cases.
Example: Using a Break to Exit from a Specific Case
#include <stdio.h> int main() { char grade = 'B'; switch (grade) { case 'A': printf("Excellent!\n"); break; case 'B': case 'C': printf("Well done\n"); break; case 'D': printf("You passed\n"); break; case 'F': printf("Better try again\n"); break; default: printf("Invalid grade\n"); } printf("Your grade is %c\n", grade); return 0; }
Here, the cases ‘B’ and ‘C’ share the same code block. The break statement is used after the shared code block to exit the switch statement.
In the next article, I will discuss Continue Statement in C Language with Examples. In this article, I try to explain the break statement in C language with examples. I hope you enjoy this Break Statement in C Language with Examples article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.