Javatpoint Logo
Javatpoint Logo

C break statement

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:

  1. With switch case
  2. With loop

Syntax:

Flowchart of break in c

c language break statement flowchart

Example

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:

  • The loop (while or for) or the switch (or a series of "if-else" statements) begins execution.
  • The program analyzes the condition within the "if" statement that includes the "break" statement throughout each iteration of the loop or each check in the switch.
  • If the condition is "true", the "break" statement is performed.
  • When a "break" statement is found, the program immediately quits the loop or switch block, skipping any remaining iterations or checks.
  • The program continues to execute the code following the loop or switch

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:

  • Simple Loops
  • Nested Loops
  • Infinite Loops
  • Switch case

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 Statement

When a loop or switch statement is executed, the break statement in the C programming language is used to stop it. It offers several features:

  • Loop termination: The break statement allows you to exit a loop prematurely based on a specific condition. It can help you save execution time by avoiding unnecessary iterations when the desired condition has been met. It provides a way to break out of nested loops as well.
  • Switch statement termination: In C, the break statement is commonly used within a switch statement to terminate the execution of the entire switch block. Without the break statement, execution would continue to the next case, potentially leading to unintended behavior.
  • Enhanced control flow: By using the break statement, you have fine-grained control over the flow of your program. It allows you to conditionally exit loops or switch statements based on specific criteria, providing flexibility and control.
  • Code organization and readability: Properly using break statements can lead to cleaner & more readable code. It lets you convey your goal by exiting a loop when a given condition is fulfilled, making the code easier to understand & maintain.
  • Error handling: The break statement can be helpful in error-handling scenarios. For example, if an error condition arises during a loop iteration, you may use the break to quit the loop and handle the problem correctly.
  • Efficiency: When dealing with large loops or switch statements, using the break statement can improve the efficiency of your code. Terminating the loop or switching early unnecessary computations can be avoided, leading to faster execution.

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.





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA