Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, March 21, 2020

break Statement in C

break statement: The break is a keyword in C, which is used to bring the program control out of the loop.

break statement | welcome2protec.com


We learned about Continue statement in previous tutorial. In this tutorial, we will learn to use of break statement with the help of examples.



How does a break Statement work in C?

  • The break is a keyword in C, which is used to bring the program control out of the loop. When a break statement is encountered inside a loop, the control directly comes out of loop and the loop gets terminated. It is used with if statement, whenever used inside loop.
  • This can also be used in switch case control structure. Whenever it is encountered in switch-case block, the control comes out of the switch-case(see the example below).

Syntax:

//loop or switch case   
break;  

Flow Chart of break Statement:

Flow chart of break statement in C | welcome2protec.com
Flow chart of break statement in C | welcome2protec.com

Example 1: break in a for loop

 #include <stdio.h> 
 #include <conio.h> 
 void main ()  
 {  
  int num, i;
  clrscr();
  printf("Enter any number between 1-100:");
  scanf("%d", &num);  
  for(i = 1; i < 100; i++)
  {
    printf("%d\n", i);
    if(i == num)
     break;
  }
  printf("Loop skipped since i == num");
  getch();
 }

Output

Enter any number between 1-100: 5 
1
2
3
4
5 
Loop skipped since i == num

Example 2: break in a for loop

/* Program to check to accept 10 nos from user
 * but if no entered is greater than 100, exit */
#include <stdio.h>
#include <conio.h>
void main ()  
{  
 int x, i;
 clrscr();
 for(i = 1; i <= 10; i++)  
 { 
  printf("To exit for loop you must enter no > 100: %d\n", x);
scanf("%d ", x); if(x > 100) break; else printf("Now loop is terminated since, you've entered no > 100: %d\n", x); } getch(); }

In above, If at any point of time, the user enters no greater than 100, the for loop is terminated, even if the iteration are left.

Output

Now loop is terminated since, you've entered no > 100: 105

Example 3: break in a while loop

#include <stdio.h> 
void main ()  
{  
    int i = 1;  
    while(1)  
    {  
        printf("%d  ",i);  
        i++;  
        if(i == 10)  
        break;   
    }  
    printf("loop skipped since condition (i==10) is returned true");  
}

Output

1 2 3 4 5 6 7 8 9 loop skipped since condition (i == 10) is returned true

Example 4: break in do...while loop

#include <stdio.h>
int main()
{
 int n = 10;
 do{
  printf("n = %d\t", n);
  n = n-1;
  if(n == 5)
  break;
 }while(n > 0);
 printf("\nOutside loop");
 return 0;
}

Output

n = 10		n = 9		n = 8		n = 7		n = 6
Outside loop

Example 5: switch...case

#include <sdtio.h> 
int main(){    
  int number=0;     
  printf("enter a number:");    
  scanf("%d", &number);    
  switch(number){    
   	 case 10:    
 		printf("number is equals to 10");    
 		break;    
 	 case 20:    
 		printf("number is equal to 20");    
 		break;    
   	 case 30:    
  		printf("number is equal to 30");    
 	 	break;    
 	 default:    
 		printf("number is not equal to 10, 20 or 30");    
    }    
    return 0;  
 }

Output

Output 1:
 enter a number:4
 number is not equal to 10, 20 or 30
 
Output 2:
 enter a number: 20
 number is equal to 20



Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us