In C programming language, the Break and Continue are flow control statements. Also known as jump statements. While both statements optimize the loops by enabling precise control over program execution, they serve different purposes.

Now let us go ahead to understand what break and continue statements with examples, their application, and how to use them.

What are Break Statements?

A break statement will exit the loop prematurely. When used in a switch statement it can exit the switch.

In other words, if you place a break statement in the loop body and specify a condition, you decide the moment to terminate the loop. This means the break statement will halt the execution of the loop and exit the current loop when the specific condition is met. Remember that, it is always good to get hands-on training through a C Language Course in India.

The syntax of the break statement in C:

 

How to use a break statement in a Loop?

Here are some rules to remember while using break statements.

  • You must write a break statement inside a loop. Compilation error will occur if it is outside the loop.
  • Generally, we use the break statement with a condition (if statements).
  •  In case, your code has a nested loop, the break statement will only terminate the innermost loop.

Let us take an example to understand the use of break statement.

Break and continue statement in C

Output 

Let us discuss this example, here we declare a variable i of integer data type in the main() function. The loop aims to print integers from 1 to 10, the initial value is 1 and the condition is that the value should be less than or equal to 10. Additionally, after every iteration, the value will be incremented by 1.

So, the code will print all the integers from 1 to 10. But in line 7 we introduced another condition (if statement). So in addition to printing all the integers between 1 to 10, the code will terminate if the value of the integer is equal to 8.  This we can see in the output.

Working of the break statements.

The working of the break statement is further clarified with the flow chart. You can see how the control flows through the loop and reaches a break statement.

The program checks a specific condition during each iteration. If the condition of the break statement is True, the control exits the loop.

Note that the loop will terminate, regardless of whether the condition of the loop is met.

The program will then continue with the code after the loop.

Break and continue statement in C

You can also check out the Functions in C Programming to enhance your understanding.

What are Continue Statements?

The continue statements do not terminate the loop. Instead, it allows us to selectively skip an iteration and continue with the loop without terminating it. This means, that if the specified condition is met, the iteration will be skipped, and the loop proceeds further to the next iteration.

The syntax of the continue statement in C

How to use the continue statements in the loop?

  • Similar to the break statement, we use the continue statement inside the loop. If used outside the loop, you will get a compilation error.
  • It is to be used along with condition statements (if statement).
  • When used in a nested loop, the continue statement will continue the next iteration of the nearest loop. 

Now let us use the continue statement in the example mentioned above.

Break and continue statement in C

Output

Similar to the previous example we created a for loop to print the value of integers. Here initial value is 1, and the loop condition is to check if the value is less than or equal to 10. Now, when this condition is met the loop moves to check for if condition in line 7. If not then the loop will print the value and the increment operator will increase it by 1 before net iteration.

Now, the difference here is that when i==5 is met, the continue statement will be executed and the program will skip the number 5 and print the remaining integer till it is less than or equal to 10. You can see this in the output, all integers from 1 to 10 are printed except the number 5.

Also, refer to Programming Languages to know which languages are in demand in 2024.

Working of the continue statements.

Let us further cement the logic behind the continued statement. Refer to the flow chart below, you will notice that similar to the break statement, the loop condition is checked to continue the iteration. When the continue statement is met, the remaining code is skipped for that iteration, and the control then jumps to the next iteration. This means the loop continues till it completes its iteration. 

 

Tips for using Break and Continue Statements in C.

Here are some best practices to keep in mind when using break and continue statements

  1. Avoid overusing them, it harms the readability.
  2. When used mention comments explaining their purpose
  3. Remember to test the loop after adding the statements
  4. To avoid any issues, make sure to check loop variable updates

Importance of Break and Continue Statements in C

Let us go through a few cases where we can use break and continue statements:

  • Consider a program that gets real-time data from a server and processes it. Now if we encountered an error in the program, it should stop fetching more data. Here a break statement is used to exit the loop of fetching data.
  •  In a situation where you want to display details of 10 employees with employee numbers, and you want to remove one employee number because it is invalid. In this scenario, you can use the continue statement. 

Looking to upskill yourself?

Break and continue statement in c

Choose a C++ Certification Course and stand out from the crowd. Get 100 percent practical training with Data structures and algorithms. Gain experience by solving 350+ Real-life coding problems. Embark on the journey with Henry Harvin, leading Ed-Tech trusted by 160+ corporates and 60+ colleges.

Up till now, Henry Harvin has trained 8965+ participants in the most powerful and portable language C++ with hands-on training given by experienced professionals. Choose to invest in yourself, and a professional certification that can open doors to industry giants. Guaranteed internship and placement assistance will propel your career in the right direction.

Conclusion

It is crucial to understand the difference between the break and continue statements. These statements act as powerful tools for programmers to control the flow of their programs. It is also necessary to remember that the logic of the program will decide whether to use a break statement or a continue statement. I hope this article helps you to understand what are break and continuous statements in C.

Recommended Reads.

  1. A Guide on Loops in C Programming- With Examples
  2. 8 Best Data Structures and Algorithms Books to Read in 2024
  3. Top Programming Languages For Data Scientists
  4. Top 50 C Programming Interview Questions You Need to Master in 2024
  5. 15 Best Programming for Non-Programmers Courses in India: 2024 [Updated]

FAQs

Q1. Are break and continue statements in C interchangeable?

Ans- No, they are not interchangeable. They serve different purposes.

Q2. In which loop constructs do we use a break and continue statements in C?

Ans- We can use both the break and continue statements in various types of loop constructs like:

  • For loops
  • While loops
  • Do-while loops

Q3. Can we use continue statements in switch statements?

Ans- No, continue statements do not apply to switch statements.

Q4. When to use a return statement?

Ans- When you want to stop the execution of the function and return the control to the calling function you can use a return statement. But remember that, this will also terminate the loop.

Q5. Why do we use control statements in C?

Ans- The control statements in C programming language help us to specify the control flow of a program. 

Join the Discussion

Interested in Henry Harvin Blog?
Get Course Membership Worth Rs 6000/-
For Free

Our Career Advisor will give you a call shortly

Someone from India

Just purchased a course

1 minutes ago
Henry Harvin Student's Reviews
Henry Harvin Reviews on Trustpilot | Henry Harvin Reviews on Ambitionbox |
Henry Harvin Reviews on Glassdoor| Henry Harvin Reviews on Coursereport