Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

break statement in C programming Language

Posted on January 1, 2018September 22, 2019

Table of Contents

  • break statement in C Language
    • break-in for loop
    • break in while loop
    • Break in Do-while loop
    • Break  in the switch
    • Similar post

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

"<yoastmark

How to work break for loop in C

"<yoastmark

 

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

"<yoastmark

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes