C Language

break statement in C programming Language

break statement in C Language

In this article, we discuss the break statement in C Language and how to use break statements.
We can use break in loops (while, do-while, for). and switch in C language. The break immediately terminates the loop and controls the move to the next statement following the loop.
The break is used with decision-making statements (check the boolean expression) such as if…else

Syntax of the break

break;

Flowchart of break

How to work break for loop in C

 

How to work break  while loop in C

How to work break dowhile loop in C

Example

break-in for loop

We can use break statement with for loop. In the following examples, we can see the usage of break-in “for loop”.

Program 1

When the above code is executed, The following output is displayed

1
2
3
4
5

 

This is a simple program to display from 0 to 10 positive number. But, When becoming i=5, the break executes and the loop is terminated.

Program 2

 

When the above code is executed, The following output is displayed

Here, the first natural numbers are:
1
2
3
4
5
The sum is: 15
terminate the program

 

This program helps to find the sum of the first 10 positive number using sum=sum+i. But, when i==5, loop flow is terminated using break statement.

program 3


When the above code is executed, The following output is displayed

case 1

Enter any number
3                               // you can enter 3 number
enter number: 1
6                               //this is the first number
enter number: 2
7                                //this is a second number
enter number :3
8
Total is: 21            // this is a total of 3 numbers

 


case 2

Enter any number
5                           // you can enter 5 number
enter number: 1
43                        //this is the first number
enter number: 2
23                        //this is a second number
enter number :3
0                           //this is the third number

 


In this program, the third number is 0 so the loop is terminated. It shows the total number.

Total is: 131


break in while loop

 

We can use break statement with while loop. Here is an example of usage of the break-in “while loop”.

Program 1



When the above code is executed, The following output is displayed


Program 2

When the above code is executed, The following output is displayed




Program 3


When the above code is executed, The following output displays

Enter any number
5
enter number :1
45
enter number :1
67
enter number :1
0
Total is :112

 

Break in Do-while loop

We can use break statement with while loop. Here is an example of usage of the reak-in “while loop”.

Program 1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=0;
    do{
        if(i==5){
            break;
             i++;
        }
        printf("%d",i);
        i++;
    }while(i<=10);

getch();
    return 0;
}

When the above code is executed, The following output displays

01234

This is a simple program to display from 0 to 10 positive number. But, When becoming i=5, the break executes and the loop is terminated.

Break  in the switch

We can use switch statement with for loop. Here is an example of uthe sage of switch.

 

When the above code is executed, The following output is displayed

1. When a value of 1 is entered,this output is displayed.

Enter the value :
1
you have entered value 1:
Exit from the loop

 

2. When a value of 2 is entered, this output is displayed.

Enter the value :
2
you have entered value 2:
Exit from the loop

 

3. When a value of 3 is entered, this output is displayed.

Enter the value :
3
you have entered value 3:
Exit from the loop

 

4. When a value of 4 is entered, this output is displayed.

Enter the value :
5
you have entered value 5:
Exit from the loop

 

5. When the input is any other number, this message is displayed

Enter the value :
5
your input other than 1,2,3 & 4:
Exit from the loop

 

You always need to use the break  in a “switch case block” at the end of the case. When that case statement is executed, break terminates the loop.

Otherwise, after a case block is executed, the next case blocks will be executed without the termination of the previous one.

For example, if we don’t use the break after every case block, then the output of this program would be:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int value;
    printf("Enter the value:\n");
    scanf("%d",&value);
    switch(value)
    {
        case 1:printf("you entered 1:\n");
        //break;
        case 2:printf("you entered 2:\n");
        //break;
        case 3:printf("you entered 3:\n");

        case 4:printf("you entered 4:\n");
         //break;
        default:
            printf("you input other than 1,2,3 & 4");
        break;
        //break;
    }
    printf("\nExit from the loop");
    getch();
    return 0;
}

When the above code is executed, The following output is displayed

Enter the value  :
1
you entered  1 :
you entered  2 :
you entered  3 :
you entered  4 :
you input other than 1,2,3 &4:
Exit from the loop

 

Similar post

Continue in C Language

Continue in Python

Pass statement in Python

Break  in Python

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.