break statement in C Language with Example programs

Spread the love
break-statement-in-c-programming-withe-example-programs

Introduction:

We have looked at the while loop, for loop, and do while loop in our earlier articles, In today’s article we will learn about the break statement in c language and how and where we can use the break statement. So let’s get started.

Break statement in C language:

The break statement is used to terminate a loop or switch statement. You can terminate a loop even before the loop condition become False.

As we discussed earlier in our loops tutorials, The loops are used to iterate over a block of code until the loop condition becomes False. However, there are situations when we wish to end the loop even before the loop condition is false. The break statement can be used in these situations to end the loop.

📢 We can use the break statement to terminate the Loops ( any loop – while, for, and do-while) and switch statements in the C programming language

Syntax of break statement:

Break Statement Example Usage:

Break Statement Flow Diagram:

break-statement-flow-diagram-in-c-language

Use the break keyword inside the body of the loop or the body of the switch statement.

The break statement will terminate the loop or switch and control will come out of the loop or switch.

📢 We can also use the break statement inside the Nested Loops and the Nested switch statements. If we use break statement with Nested loops,etc then only the inner-most loop will terminate.

Program to demonstrate break statement in C language:

Let’s write a C program to search for a number in a given array of numbers. Once we found the number, we are going to break out of the loop. As we no longer need to check the remaining elements in the array.

We are going to use the Linear search for simplicity.

📢 The C-Array’s are used to store multiple values of a similar datatype. We are going to discuss the array’s in upcoming tutorials.

Save the above program as findNumber.c and compile and run the program using your IDE.

Program Output:

Program Explanation: Find a number in an array program Algorithm:

  • We have used an array ( arr) to store the 10 values from 1 to 10.
  • And asked the user for a number ( num) to search.
  • Then we calculated the size of the array using the sizeof(arr)/sizeof(arr[0]); and stored the result in arrSize variable.
  • Once we got the above details, We Iterated over the array using the for loop.
  • At Each Iteration, we check if the array element at the index i and the num are equal.
    • If it is equal, Then we found the number. So we are displaying the Number found message and immediately we are using the break statement to terminate the loop. As we found the num, We no need to look at the other elements in the array.
    • If it is not Equal, Then we go to the next Iteration.
  • Until we find a match or get to the end of the array, the above step will be repeated.
  • Once we come out of the loop, We are checking if the i is equal to arrSize, If we don’t find a match, Then we will iterate over all elements of the array, So the i value will reach the length of the array, which is arrSize. So if we don’t find the match, Display the Not Found message.

One thing to note here is, If we find the number( num) then we are not iterating over the remaining elements in the array. The break statement is being used to end the loop.

In the worst case, if the provided number happens to be the last number in the array, Then we will need to traverse over every element of the array.

Now Let’s look at how we can use the break statement with different loops and switch case.

  • Break statement with a while loop
  • Break statement with for loop
  • Break statement with do while loop
  • Break statement with a switch statement

Break statement with the while loop in C:

We can specify the break statement to exit out of the while loop. Here is an example program.

Program: while loop with break statement example:

The while loop is going to iterate from 1 to 10. and we are going to terminate the loop once we reached number 3 using the break statement.

Program Output:

Break Statement with for loop:

The above number search program used the for loop to iterate over the array elements and once we found the number, terminated the loop using break.

Let’s look at another example, We are going to have an Infinite for loop and stop the loop when the count value reaches 10.

Learn more about the Infinite loops in the following article

Program to understand for loop with break statement:

Program Output:

As you can see we have an Infinite for loop, which has a counter variable, The counter variable starts from 0 and for each iteration increments the counter by 1. We want to stop the loop once the counter reached the value of 10.

So we are checking the value of the counter with 10 at each iteration and at the tenth Iteration, the value of the counter became equal to the 10. So we stopped the loop using the break statement.

Do while loop with break statement in C:

Similarly, we can terminate the do-while loop using the break statement.

Here is an example.

Program to understand do while loop with break:

Program Output:

Break Statement with Switch Statement:

We have looked at the Switch Statement in our previous article. The switch is a mutli-way decision-making statement. The switch statement has the cases. If the given switch condition matches any of the cases, the respective code block will be executed. To come out of the case, We use the break statement.

📢 The break statement is one of the essential parts of the switch statement, As it helps to come out of the switch case after executing the desired case. Forgetting to include the break statement after the case can lead to the execution of subsequent cases. So make sure to add the break statement whenever it is needed.

Match Numbers using the switch statement and break statement in C language:

Program Output:

Break Statement with the Nested Loops in C language:

We can use the break statement in the Nested loops. When we use the break in the Nested loop, Only the inner-most loop will Terminate. and control comes out to the next loop.

Note that, Using the break statement in the Nested loop won’t terminate all Loops. It will only terminate the inner-most loop where the break is used.

Let’s look at an example program.

Example Program to understand break with Nested loops:

We are going to create a Nested loop. The Outer loop will iterate from 1 to 5 and at each Iteration The Inner loop will iterate from 1 to 3 ( Actually loop will go up to 5, but we are using break statement to terminate the loop once the value of b reaches 3)

Program Output:

As you can see from the above program for each iteration of the outer loop, The inner loop is iterating from 1 to 3. And Once the b reaches 3, We are terminating the Inner loop.

Break Statement with the Nested switch cases:

Similar to the nested loops, If we use the break statement in Nested switch statements, The innermost switch statement will be terminated. And control comes out to the next switch statement.

Program to understand the break statement with switch case:

Here is a Restaurant order program. Here we used two switch statements (Nested switch case).

Program Output:

As we have discussed earlier, The break statement won’t terminate all switch statements. It will only stop the switch statement where break is specified.

Conclusion:

We have looked at the break statement in C language, and we also learned where we can use break statement and how it will affect the flow of the program. Then we saw how we can use break statement with different loops and the switch statement with example programs. Finally, We looked at break with Nested loops and nested switch statements.

If you reached it so far, Then you would also enjoy our other tutorials, Here is the Index of the tutorials.

Loops and break statement practice programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] Break Statement in C with Example Programs […]

  2. […] have looked at the loops and break Statement in earlier posts. In today’s article, We are going to discuss about the Continue Statement in C […]

  3. […] Break Statement in C with Example Programs […]

  4. […] the max is evenly divisible without any remainder, Then max is our LCM. Print it and break the […]

Leave a Reply