C++

Break statement in C++

A break statement in C++ is a statement that controls the loop in such a way as to terminate it. It is used to stop the present execution so that the next one can be executed. While reading the code, when the compiler reads the break statement, the execution of the loop is stopped immediately, and then the control is transferred outside the loop for the other statements to get executed. The break statements are used mainly in two conditions. When the user doesn’t know about the iteration numbers in the program. And secondly, when a user wants to terminate the program according to some given condition. This article will explain the variance in the usage of the break statement.

To understand the break statement on the Linux operating system, you need to have Ubuntu running on your system. For this purpose, install Virtual Box and configure it. Now add the Ubuntu file to it. Before adding, the file can easily be installed via the internet through a website that will take some hours to be completed. Run that file, and after successful configuration, you can now use Linux flavor easily. During configuration, make sure that the user is created; this is necessary to create a user so that you can access all features of Ubuntu.

Note: We have used Ubuntu 20.04; all these codes explained here are executable on any version. For the implementation, you need to have a text editor and must have access to the Linux terminal because we will be able to see the output of the source codes on the terminal through the query.

The syntax is very simple as it is only a keyword.

Break;

Working in C++

The break statement stops executing the point where it is defined and starts executing the other statements. If you have already mentioned a condition in the program, it executes the program according to that condition. The compiler first checks the condition. If the condition is true, the conditional statement is executed. If the break statement is mentioned after the condition, the program is immediately stopped. Otherwise, the loop continues to iterate till the condition remains true; as it becomes false, the program is terminated.

Now we will mention and explain some elementary examples to add more knowledge regarding the break statement.

Example 1

The first example shows the simple linear search working in a simple function without a break statement. After that, we will incorporate the break statement in the same program to depict their dissimilarity.

Now coming back to the example, first, we will use the library to read and easily write in the program.

#include<iostream>

Then directly jumping on the main program, we have initialized an array, and then a variable is assigned a number that is to be searched in an array and finally a function call at the end. This call has the array, total number, and that number that is to be searched. To perform the searching function, a function is used here that will accept all the arguments from the main function. A for-loop is used to search the position of that number. The display statement will show the index number, and at the same time, the process of searching is continued at the end of the loop till it is terminated according to the condition.

Save the code in the file with the name extension of “.c”. For the output, navigate to the terminal of Ubuntu. As each code is executed by a specific compiler, here for C++, we need a G++ compiler for the compilation and execution of the code. The ‘break. c’ is the filename.

$ G++ -o break break.c

$ ./break

You can see that the number ‘3’ is present on the 5th location in an array. Now, we will make some changes to the code of that program. For instance, we have used the number ‘3’ twice in the array to see the result accordingly.

Apply the same method to get the desired output. As expected, the result shows the position for both the indexes having the same number on them.

Now, coming towards the main idea, the ‘break’ statement in the code will be used just after the display statement inside the loop. This will function in such a way that as the number is found, the loop will be terminated. It will not wait to search the number again in the loop in another position. The position that comes earlier will be displayed, and the loop will stop the execution process.

As on compilation and running the program, you can observe that the first position is shown only from the output.

Example 2

In this example, we have used nested for-loops to explain the phenomenon of the break statement. The break statement is declared inside the innermost loop. So the control will come out from the innermost loops on the encounter of the compiler with the break statement.

In the code, the main program contains an outer for loop having iterations up to 7, which means the outer loop will execute 7 times. Inside this loop, declare an inner for loop; this will iterate up to 7. And inside this loop, an if-statement is used to check if the inner loop variable becomes greater than 5, then breaks the iteration and comes outside the loop. If the iteration is less than 5, print the “#” hash sign. It means in each line; the hash sign will appear 5 times.

If ( j >5)

Break;

Execute the code through the compiler in the terminal. You will see that 7 lines are executed according to the condition of the outer loop. In the case of the inner program, you can see that it was programmed to run 10 times.

Example 3

This is an example of user interaction. A while loop is used in the main program. The program will calculate the numbers that are gained through the user. We will take two variables; one is to get the numbers from the user, and the other one is to count the numbers and calculate the sum. We have applied a condition here that if a user enters a negative number, then terminates the further execution and calculates the sum of the numbers entered till now. When the break word is used, the control will come out from the if-statement, and then the sum will be calculated.

If (number < 0)

Break;

Now we will check the result of this source code. When we execute the code, the system will require value from us. Now we will enter the number one after another. The system will continue asking the number until we enter a negative value. Otherwise, this process of getting value will continue.

As we have entered ‘-4’, the program is terminated.

Conclusion

This article explains the working of a break statement in the code. The break statement is mostly used inside the for-loop, in the case of an array, searching, and comparison processes. Each example regarding the usage of the break statement is explained briefly in this tutorial. I hope it will be enough effort for the explanation of the break statement.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.