break statement in C

break statement in C

The break statement terminates the execution of the nearest enclosing loop (whiledo while or for) and switch body. After termination of the loop or switch body, control passes to the statement that follows the terminated statement. Before using the break statement you should remember, the break statement shall appear only in a switch body or loop body.

Syntax:

break;

Flowchart of break (Jump statement)in C

 

break statement

 

How does break work in C?

Let see an example code to understand the working of the break. The below-mentioned code adds positive enter numbers. If the user enters any negative number, then the program will be displayed the sum of all positive numbers and terminate.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int data = 0;
    int sum = 0;

    printf("\nEnter negative number to display sum \n\n");

    while(1) //Infinite loop
    {
        printf("\nEnter the number: ");
        scanf("%d",&data);

        if(data < 0)
        {
            printf("\nExit from the infinite loop\n\n");
            break;
        }
        else
        {
            sum+= data;
        }

    }

    printf("Sum of number = %d \n\n",sum);

    return 0;
}

Output:

c break

 

Importance of break statement in switch case

In below example, if iChoice is equal to 1, all statements of the body are executed because there is no break statement appear in the body of the switch case. if ichoice equal to 2 then control jump to case 2 and execute all the below case since there is no break.

#include <stdio.h>

int main()
{
    int iChoice   = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch ( iChoice )
    {
    case 1:

        printf("case 1 !\n");

    case 2:

        printf("case 2 !\n");

    case 3:

        printf("case 3 !\n");

    default:
        printf("default !\n" );

    }

    return 0;
}

Output:

switch c code

 

We can resolve this problem using the break statement, see the below code.

#include <stdio.h>

int main()
{
    int iChoice   = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch ( iChoice )
    {
    case 1:

        printf("case 1 !\n");
        break;
    case 2:

        printf("case 2 !\n");
        break;
    case 3:

        printf("case 3 !\n");
        break;
    default:
        printf("default !\n" );

    }

    return 0;
}

Output:

Enter your choice = 1
case 1 !

Enter your choice = 2
case 2 !

 

As I mentioned at the beginning of this article is that within the nested loop or nested switch case, the break statement terminates the execution of the nearest enclosing loop or switch body.

Let consider an example to understand the above-mentioned statement. Suppose there are three loops (while loop) X, Y, and Z. The loop Z nested inside loop Y and Y is nested inside the loop X. There are three scenarios comes after writing the break inside these loops.

1. break inside loop Z: It will break only loop Z, control still inside the loop Y.

#include<stdio.h>


int main()
{
    int loopX = 0,loopY = 0,loopZ = 0;

    while(loopX < 3)
    {

        while(loopY < 3)
        {
            while(loopZ < 3)
            {

                printf(" Value of loopZ =  %d\n\n",loopZ);
                loopZ = 3;
                break;
            }

            printf(" Value of loopY =  %d\n\n",loopY);

            ++loopY;
        }

        printf(" Value of loopX =  %d\n\n",loopX);

        ++loopX;
    }
    return 0;
}

Output:

 

2. break inside loop Y (but not inside the loop Z): It will break only loop Y, control still inside the loop X.

#include<stdio.h>


int main()
{
    int loopX = 0,loopY = 0,loopZ = 0;

    while(loopX < 3)
    {

        while(loopY < 3)
        {
            while(loopZ < 3)
            {

                printf(" Value of loopZ =  %d\n\n",loopZ);

                ++loopZ;
            }

            printf(" Value of loopY =  %d\n\n",loopY);

            loopY = 3;
            break;
        }

        printf(" Value of loopX =  %d\n\n",loopX);

        ++loopX;
    }
    return 0;
}

Output:

break statement in c

3. break inside loop X: It will break the only loop X.

#include<stdio.h>


int main()
{
    int loopX = 0,loopY = 0,loopZ = 0;

    while(loopX < 3)
    {

        while(loopY < 3)
        {
            while(loopZ < 3)
            {

                printf(" Value of loopZ =  %d\n\n",loopZ);

                ++loopZ;
            }

            printf(" Value of loopY =  %d\n\n",loopY);

            ++loopY;
        }

        printf(" Value of loopX =  %d\n\n",loopX);

        loopX = 3;
        break;
    }
    return 0;
}

Output;

break

 

In below example code, I have implemented a break only in inner switch case( in case 1). What I want to show here if a break executes then it only terminates the nearest enclosing switch body.

When you compile the code, you will find that only the inner switch case will be terminated and break of the inner switch case will not affect the outer switch case body.

#include <stdio.h>

void NestedSwitchDemo(int input1, int input2)
{

    switch (input1 )
    {

    case 1:

        printf("\n Your enter choice is 1\n");
        switch (input2 ) //inner switch case body
        {

        case 1:

            printf("\n Enter Sub choice is 1\n");
            break;
        case 2:

            printf("\n Enter Sub choice is 2\n");
            break;

        }

    // break statement missing
    case 2:

        printf("\n Your enter choice is 2\n");

    // break statement missing

    default:
        printf("\n Bad Input !\n");


    }

}
int main()
{
    int iChoice  = 1;
    int iSubChoice = 1;

    //Both choice and subchoice are 1
    NestedSwitchDemo(iChoice,iSubChoice);


    return 0;
}

Output:

break in c

 

Recommended Articles for you: