C Programming

Break Statement in C

“To remove the program control from the loop, we utilize the C language term “break.”The loop’s iterations cease as immediately after the break statement is detected inside the loop, and control is instantly handed onto the first statement following the break.

In this tutorial, we will practically implement the break statement to execute various example codes in C.”

Example#1: Break Statement With For-Loop

In this illustration, we will learn to use the break statement in a for-loop. The below-mentioned code snippet is executed to apply the break statement in C programming for the for-loop.

The code begins by including the “stdio.h” header file in the program. This header file contains all the needed operations that are relevant to input or output, which could be included in our program. Then we used the “main()” function with “int” datatype. When we invoke the “int main()” function, it means that this function will return us integer datatype value(s). A variable has been declared with an integer datatype as “int x”. In the next line of the program, the “for-loop” is initialized. In this loop, we have assigned the variable “x” the value “5”, which is separated by a semicolon; we then defined the condition which says that “x” is less than “20”, and the last fragment is declared to increment the value of “x”. So as a whole, this loop states a condition for the value of “x” that starts from “5” and until it is less than “20”, increment the value of “x” with each condition check. The brackets of the for-loop start wherein we have employed a “printf()” function. The “printf()” will display whatever we would supply to it as input. Within its round brackets, we have specified the variable “x: %d\n” then a comma and “x”. The first “x” will be printed as it is; the “%” is a datatype specifier used in C programming, which tells us the type of information it will store and exhibit.

Here we have added “%d”, which indicates that the type of data is decimal integers. The “\n” represents jumping to the next line. And “x” will display the values after every increment. After that, we have specified an “if-statement”. This instructs the control if the value of “x” equals “9,” then executes the specified operation inside this conditional statement. Within the if-statement, “break” is specified. So if the condition is met, it will immediately terminate the loop. After breaking the loop, the control will move to the “printf()” function and will display the statement we have specified in it. “Return 0” means that the compiler has executed the programming successfully for what it was intended to perform.

In the output image presented above, you can see that the values of “x” have been displayed from the value “5” to “9”. When the value became “9”, the loop immediately terminated and didn’t check the rest of the values. Finally, printed the Statement defined out of the loop.

Example # 2: Break Statement With While-Loop

For this instance, we will be using the “break- statement” inside the “While-loop” for terminating the loop once the particular condition is matched. The script we have generated for the programs can be viewed in the snapshot below:

Let’s walk through this script.

Here, the “main()” function begins; we have first declared an “int” type variable “y” and assigned it the value “5”. The “while-loop” starts with “while(1)”. This “1” in the while loop means the same as “while(True)”, which refers to looping forever. This loop will take in the value of “y” and check the condition/ statement we have identified inside the loop.

The first task contained by the loop is to just show the value of “y”. After printing the value, the control will move to the next line, where it has to increment by 1 in the current value of “y”. Moving onward, now we have specified an “if- statement”. This will determine whether the value of “y” following the increment is “12.” in case the value id is found equal to “12”, terminate the loop by applying“break” to it.

Once the control is out of the loop, it searches for any other operation that needs to be executed. A “printf()” function is specified there, which holds the statement “We are out of the Loop” to be exhibited on the output screen.

The resultant window displays a list of values for variable “y” from “5” to “11”. Once the value became “12”, the loop terminated and printed the statement declared out of the loop.

Example # 3: Break Statement With Switch-case

Our last example will demonstrate the usage of “break-statement” in switch cases in C.

We have created the following example program to comprehend the application of “break” in “Switch-cases”.

After including the header file and invoking the “main()” function, we have declared a variable “n”. Then we use the “printf()” function, which stores the statement “Enter a number”. To take the input from the user, the “scanf()” function is called with “%d” for identifying the datatype and “&n” for taking the input value. We then utilized the “switch()” condition. Within the round brackets of the switch, we specified the variable “n”, which will take in the number that is provided by the user. In the curly brackets of the switch, we use “Case1” and the “printf()” function, which contains the output “you have entered number1” and “\n”.In the next line, we have used“break.” If “Case1” doesn’t match the condition, the control will move to “Case 2”.

In this, we have a “prinf()” function with the statement “you have entered number 2”. The next line will “break” the “switch()” if “Case2” meets the condition. In the same way, “Case3″ is specified. Lately, a “default:” case is declared, which will execute if none of the cases met the particular condition and will exhibit the statement in which we have written “input number is other than 1,2,3”, then the switch condition will be closed.

In the above snapshot, you can see the user enter the number “1”, which means case 1 is encountered and output is printed “you have entered number 1”. Due to the “break” statement, no other case will be checked.

In the output image presented above, you can see the user enter number “4”. It means no case conditions matches, and the compiler goes to the default statement, which is “input number is other than 1,2 & 3”. So, no “break” statement is encountered because conditions are not matched.

Conclusion

The Break statement is used when we don’t know what is the actual number iteration for the loop or we need to terminate the loop based on some conditions. In this article, we have explained 3 different examples with different types of loops. The first illustration is based on using a break statement in “for-loop.” The 2ndexample is for using a break statement in “while- loop.” In this 3rdinstance, multiple break statements are used in “switch-case” conditions. Any of these strategies can be employed depending on the task’s specifications.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.