Home Show/Hide Menu
C Tutorials
➜ C Language
  - Features of C
  - First C Program
  - Compilation in C
  - C Character Set
  - Tokens
  - Keywords
  - Identifiers
  - Constants
  - Operators
  - Data Types
  - Variables
  - Type Casting
  - Comments
  - Input/Output
  - Escape Sequence
  - Programming Errors
➜ Control Statements
  - if Statement
  - if-else Statement
  - Nested if-else
  - else-if
  - switch Statement
  - Loop
  - while loop
  - do while loop
  - for loop
  - break
  - continue & goto
  - Nested Loop
  - Infinite Loop
➜ Functions
  - What is function
  - Call by Value & Reference
  - Recursive function
  - Storage Classes
➜ Array
  - 1-D Array
  - 2-D Array
  - Return an Array
  - Array to Function
➜ C Pointers
  - Pointers
  - Pointer to Pointer
  - Pointer Arithmetic
  - Dangling Pointers
  - sizeof() operator
  - const Pointer
  - void pointer
  - Dereference Pointer
  - Null Pointer
  - Function Pointer
  - Function pointer as argument
➜ Memory Management
  - Memory Layout of C
  - Dynamic memory
  - calloc()
  - malloc()
  - realloc()
  - free()
➜ Strings
  - gets() & puts()
  - String Functions
  - strlen()
  - strcpy()
  - strcat()
  - strcmp()
  - strrev()
  - strlwr()
  - strupr()
  - strstr()
➜ C Math
  - Math Functions
➜ Enum, Structure & Union
  - Enum
  - Structure
  - typedef
  - Array of Structures
  - Nested Structure
  - Structure Padding
  - Union
➜ File Handling
  - fprintf() fscanf()
  - fputc() fgetc()
  - fputs() fgets()
  - fseek()
  - EOF and feof()
  - fsetpos()
  - tmpfile()
  - rewind()
  - ftell()
  - lseek()
➜ Preprocessor
  - Preprocessor
  - Macros
  - #include
  - #define
  - #undef
  - #ifdef
  - #ifndef
  - #if
  - #else
  - #error
  - #pragma
➜ Command Line
  - Arguments

Break statement in C Language

In C programming language, break is a keyword that is used to terminate the loop or a case in the switch statement. It terminates the execution of the nearest enclosing of do, for, switch, or while statement in which it appears and control passes to the statement that follows the terminated statement. The break statement can be used in the following two scenarios:

  • With loop - When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. In the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.
  • With switch case - The break statement also is used to terminate a case in the switch statement. When it is encountered in switch-case block, the control comes out of the switch-case.

Break statement with loop
// Program to find the length of a given string
#include<stdio.h>
#include<conio.h>
void main()
{
   int i,length=0;
   char string[100];
   printf("Input a string : ");
   gets(string);
   for (i = 0; i < 100; i++) 
   {
   	if (string[i] == '\0')
	      break;
   	length++;
   	}
   printf("\nThe length of the string is : %d \n",length);
   getch();
}

Output :

Input a string : Hello World                            
The length of the string is : 11
Break statement with switch case
// Program to find the length of a given string
#include<stdio.h>
#include<conio.h>
void main()
{
   int num1, num2;
   char key;
   printf("Input number 1 : ");
   scanf("%d\n",&num1)
   printf("Input number 2 : ");
   scanf("%d\n",&num2)
   printf("Input a operator : ");
   scanf("%c\n",&key)
	switch (key)
	{
	   case '+':
	      printf("Result : %d",num1+num2);
	      break;
	   case '-':
	      printf("Result : %d",num1-num2);
	      break;
	   case '*':
	      printf("Result : %d",num1*num2);
	      break;
	   default:
	      printf("Invalid operator\n");
	}
   getch();
}

Output :

Input number 1 : 15 
Input number 2 : 10  
Input a operator : -                         
Result : 5
ADVERTISEMENT