C break statement ❮ Edit Details
The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. The break statement in C can be used in the following two scenarios:
- With switch case
- With loop
Syntax:
- //loop or switch case
- break;
Flowchart of break in c
Example
- #include<stdio.h>
- #include<stdlib.h>
- void main ()
- {
- int i;
- for(i = 0; i<10; i++)
- {
- printf("%d ",i);
- if(i == 5)
- break;
- }
- printf("came outside of loop i = %d",i);
- }
Output
0 1 2 3 4 5 came outside of loop i = 5
How does the break statement work?
The "break" statement works similarly to other programming languages in C programming. A control flow statement is used to exit a loop or switch statement early when a specific condition is met. The "break" statement is beneficial when you want to terminate a loop early or exit a switch block before it ends. The process of the "break" statement works in C is the same as in other programming languages:
Note: The "break" statement only affects the innermost loop or switch block which is contained within the statement. If there are nested loops or switch statements, the nearest one that includes the "break" statement will be broken out of.Without the "break" statement, the loop or switch would continue its normal execution and process all the remaining iterations or checks, even if the desired condition has already been met. The "break" statement provides an efficient way to exit loops or switch blocks when you no longer need to perform further iterations or checks.
Use of break statements in different cases with their examples:Let us go through each use case of the "break" statement in C with detailed explanations and examples:
1. Simple Loops: When a specific condition is fulfilled, the "break" statement is widely used in simple loops like "while" and "for" to stop the loop early. It is helpful when you wish to terminate the loop early due to a condition. Syntax: It has the following syntax: While loop
For loop
Example: Let's take an example to understand the use of the break statement in simple loops in C:
Output 1 2 3 4 Explanation: In this example, the "while" loop outputs the digits 1 through 4. When i equals 5, the "if" condition if (i == 5) is true, and the "break" expression is performed, forcing the loop to finish early. 2. Nested Loops:The break statement can be applied in nested loops to break out of the inner and outer loops simultaneously when a specific condition is met. It allows you to stop processing further iterations in multiple loops at once.
Syntax: It has the following syntax:
Example:
Let's take an example to understand the use of the break statement in nested loops in C:
Output (1, 1) (1, 2) (1, 3)
Explanation: In this example, the nested loops output pairs of integers ranging from 1 to 3. When i equals 2 and j equals 2, the "if" condition if (i == 2 && j == 2) is true, and the "break" statement is run, causing both loops to end at the same time. 3. Infinite Loops:An infinite loop runs continuously unless terminated by a "break" statement or another condition within the loop. In infinite loops, the "break" statement is typically used to give a means to leave the loop based on specified criteria. Syntax: It has the following syntax:
Example:
Let's take an example to understand the use of the break statement in infinite loops in C:
Output Enter a number (0 to exit): 7 You entered: 7 Enter a number (0 to exit): 5 You entered: 5 Enter a number (0 to exit): 0 Explanation: In this example, the program keeps asking the user to enter a number in an infinite loop. If the user enters 0, the "if" condition if (number == 0) is true, and the "break" statement is executed, ending the loop and terminating the program. 4. Switch Case:The "break" statement is used in a "switch" statement to exit the switch block after a particular case is executed. Without the "break" statement, the program would continue executing the code for all the subsequent cases, potentially leading to unexpected behavior. Syntax: It has the following syntax:
Example: Let's take an example to understand the use of the break statement in the switch case in C:
Output (Example 1): Menu: Option 1 Option 2 Quit Enter your choice: 2 You selected Option 2 Output (Example 2): Menu: Option 1 Option 2 Quit Enter your choice: 4 Invalid choice. Try one more time. Explanation: In this case, the program presents a menu & prompts the user to select an option. The matching "case" block is run based on the user's input. After the execution of the corresponding "case" block, the "break" statement exits the switch block, preventing the program from executing the code for other cases.
These are the different ways you can use the "break" statement in C to control the flow of your program within loops and switch statements. The "break" statement provides a powerful mechanism to exit loops and switch blocks when certain conditions are met, making your code more efficient and functional. Features of the Break StatementWhen a loop or switch statement is executed, the break statement in the C programming language is used to stop it. It offers several features:
Note: Excessive use of break statements can sometimes make code harder to understand and maintain, mainly when it is used in nested loops. It's essential to use break judiciously and consider other control flow options, such as continuing or restructuring your code when appropriate.
|