Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C Control Flow: Break and Continue Statements Explained

Mastering C Control Flow: Break and Continue Statements Explained

Understanding loop control is essential for writing efficient C programs. In this guide, we dive into the break and continue statements, showcasing how they shape loop behavior through practical examples.

C break

The break statement terminates a loop immediately when it is encountered. Its syntax is straightforward:

break;

Typically, break is used in conjunction with an if...else condition inside the loop to exit under specific circumstances.


How break Works

Mastering C Control Flow: Break and Continue Statements Explained

Example 1: Using break to Stop Input Early

// Calculate the sum of up to 10 numbers
// The loop exits if the user enters a negative value

#include <stdio.h>

int main() {
   int i;
   double number, sum = 0.0;

   for (i = 1; i <= 10; ++i) {
      printf("Enter n%d: ", i);
      scanf("%lf", &number);

      if (number < 0.0) {
         break;
      }

      sum += number;
   }

   printf("Sum = %.2lf\n", sum);
   return 0;
}

Output

Enter n1: 2.4
Enter n2: 4.5
Enter n3: 3.4
Enter n4: -3
Sum = 10.30

This program demonstrates that break stops the for loop when a negative number is entered, preventing further input and immediately displaying the accumulated sum.

In addition, break is widely used within switch statements to exit a case block—a topic covered in our next tutorial.


C continue

The continue statement skips the remainder of the current loop iteration and proceeds directly to the next iteration. Its syntax is:

continue;

Like break, it is most commonly paired with an if...else condition to control loop flow dynamically.


How continue Works

Mastering C Control Flow: Break and Continue Statements Explained

Example 2: Skipping Negative Numbers with continue

// Calculate the sum of up to 10 numbers
// Negative entries are ignored using continue

#include <stdio.h>

int main() {
   int i;
   double number, sum = 0.0;

   for (i = 1; i <= 10; ++i) {
      printf("Enter a n%d: ", i);
      scanf("%lf", &number);

      if (number < 0.0) {
         continue;
      }

      sum += number;
   }

   printf("Sum = %.2lf\n", sum);
   return 0;
}

Output

Enter n1: 1.1
Enter n2: 2.2
Enter n3: 5.5
Enter n4: 4.4
Enter n5: -3.4
Enter n6: -45.5
Enter n7: 34.5
Enter n8: -4.2
Enter n9: -1000
Enter n10: 12
Sum = 59.70

Here, the continue statement prevents negative numbers from contributing to the total, ensuring that only valid inputs affect the sum.


C Language

  1. Minterms & Maxterms in Karnaugh Maps: Clear Notation & Practical Examples
  2. Mastering Conditional Logic in C#: If, If‑Else, If‑ElseIf, and Nested If Statements
  3. C# Break Statement: How & When to Use It
  4. Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively
  5. Master C++ Conditional Statements: if, if...else, and Nested if...else Explained
  6. C++ While and Do‑While Loops – Mastering Repetition in Your Code
  7. Mastering C++ Continue Statement: Practical Examples & Loop Control
  8. Mastering C Control Flow: Break and Continue Statements Explained
  9. C Recursion Made Easy: Writing and Using Recursive Functions
  10. Mastering Python Loop Control: break & continue