Break Statement in C

Break Statement in C

In C programming, the “break” statement is a handy tool that allows you to control how your code behaves. It’s particularly useful for stopping loops and switching statements before they would normally finish. This gives you more control over your program and helps you respond to specific conditions as needed. In simpler terms, the “break” statement is like a stop button for loops and switch statements in C.

What is Break in C

In C programming, the “break” statement serves as a way to control loops. When encountered, it exits the loop immediately, allowing you to control the flow of your program. You can use it inside loops or switch statements to exit the block of code. It’s important to note that the “break” statement can only exit one loop at a time, not multiple nested loops simultaneously.

Syntax of Break in C

break;

Use of Break in C

The “break” statement in C is a tool used to exit a loop prematurely. It can be employed with various types of loops, such as “for” loops, “while” loops, or “do-while” loops, to transfer program control out of the loop at a specific point. This allows you to terminate the loop before it naturally reaches its end based on certain conditions or requirements in your program.

In C, we can use the break statement in the following ways::

  • Simple Loops
  • Nested Loops
  • Infinite Loops
  • Switch case

Example 1: C Program to use break Statement with Simple Loops


// C Program to demonstrate break statement with for loop
#include <stdio.h>
 
int main()
{
 
    // using break inside for loop to terminate after 2
    // iteration
    printf("break in for loop\n");
    for (int i = 1; i < 5; i++) {
        if (i == 3) {
            break;
        }
        else {
            printf("%d ", i);
        }
    }
 
    // using break inside while loop to terminate after 2
    // iteration
    printf("\nbreak in while loop\n");
    int i = 1;
    while (i < 20) {
        if (i == 3)
            break;
        else
            printf("%d ", i);
        i++;
    }
    return 0;
}

Example 2: C Program to use break Statement with Nested Loops


// C program to illustrate
// using break statement
// in Nested loops
#include <stdio.h>
 
int main()
{
    // nested for loops with break statement
    // at inner loop
    for (int i = 1; i <= 6; ++i) {
        for (int j = 1; j <= i; ++j) {
            if (i <= 4) {
                printf("%d ", j);
            }
            else {
                // if i > 4 then this innermost loop will
                // break
                break;
            }
        }
        printf("\n");
    }
    return 0;
}

Output

1 
1 2 
1 2 3 
1 2 3 4

Example 3: C Program to use break Statement with Infinite Loops

// C Program to demonstrate infinite loop without using
// break statement
#include <stdio.h>
 
int main()
{
 
    int i = 0;
 
    // while loop which will always be true
    while (1) {
        printf("%d ", i);
        i++;
        if (i == 5) {
            break;
        }
    }
    return 0;
}

Output

0 1 2 3 4 

In the given program, there is a potential issue where the loop condition is always set to true, which would result in the loop running indefinitely, causing an infinite loop. However, this problem is addressed by incorporating the “break” statement. By using “break,” the loop is controlled, and its iteration is limited to 8 iterations. This way, you prevent the loop from running indefinitely and ensure it terminates after a specific number of iterations, enhancing the program’s stability and predictability.

The working of the break statement in C is described below:

STEP 1: The loop begins executing after evaluating the test condition.

STEP 2: If there’s a “break” condition in the loop, it’s checked.

STEP 3A: If the “break” condition is true, the program immediately jumps to the statements below the loop, effectively ending the loop prematurely.

STEP 3B: If the “break” condition is false, the program continues executing as usual within the loop.

This mechanism allows you to control and exit the loop based on specific conditions, providing flexibility and control over your code’s behavior.

Break In C Switch Case

The “switch” case statement in C evaluates an expression and, based on the value of the expression, executes the statement associated with the matching value. Importantly, if there are multiple cases after the matching one, all of them will be executed sequentially unless stopped. To halt this sequential execution, the “break” statement is used within the “switch” case. When “break” is encountered, it exits the “switch” block, preventing further case statements from being executed. This helps control the flow of the program within the “switch” construct.

Syntax of break in Switch Case

switch(expression)
{    
case value1:
    statement_1;
    break;
    
case value2:
    statement_2;
    break;
.....
.....

case value_n:
    statement_n;
    break;
    
default:
    default statement;
}

Example of break in switch case

// C Program to demonstrate working of break with switch
// case
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char c;
    float x, y;
 
    while (1) {
        printf("Enter an operator (+, -), if want to exit "
               "press x: ");
        scanf(" %c", &c);
        // to exit
        if (c == 'x')
            exit(0);
 
        printf("Enter Two Values:\n ");
        scanf("%f %f", &x, &y);
 
        switch (c) {
        // For Addition
        case '+':
            printf("%.1f + %.1f = %.1f\n", x, y, x + y);
            break;
        // For Subtraction
        case '-':
            printf("%.1f - %.1f = %.1f\n", x, y, x - y);
            break;
        default:
            printf(
                "Error! please write a valid operator\n");
        }
    }
}

Output

Enter an operator (+, -), if want to exit press x: +
Enter Two Values:
10
20
10.0 + 20.0 = 30.0

FAQ- Break Statement in C

Q1. What is break vs exit statement in C?

Ans. The key difference between “break” and “exit()” in C is that “break” is a keyword used to immediately exit a loop or switch statement, while “exit()” is a standard library function that terminates the entire program’s execution when called. “break” controls the flow within constructs, while “exit()” ends the program entirely.

Q2. What is break in C with example?

Ans. The “break” command in C enables you to exit a loop (such as “do,” “for,” or “while”) or a switch statement prematurely, bypassing its normal completion. It can only be used within the body of a loop or a switch statement. It’s important to note that the “break” keyword must be in lowercase and cannot be abbreviated.

Q3. Why is break statement used?

Ans. The “break” statement in C is a loop control statement designed to terminate a loop. When the program encounters a “break” statement within a loop, it immediately stops the loop iterations and transfers control to the first statement after the loop, effectively exiting the loop prematurely.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment