{ A destination to learn coding }

Jump Statements

The break statement

break statement is used for termination purpose.

break is used in the iteration Statements to terminate the loops, and are also used in switch statements.

Using break in loops

See the example
#include<stdio.h>
int main()
{

  for(int i=0; i<10; i++)
  {
  if(i==5)
    break;

  printf("%d\n",i);
  }

  return 0;
}
Output: 0 1 2 3 4

Unlike continue, which continues the iteration, break causes the loop to terminate.
In the above code, if the value of i is 5 the loop will terminate.


Using break in switch

case constant:
  statement;
  break;

A break statement is used to terminate a case.
Without the use of break, the subsequent cases executes resulting in unnecessary duplication of statements.
Use of break tag makes the code more efficient.

Use of break in switch
#include<stdio.h>
int main()
{
  char a;
  printf("Enter any one of the letters, (a,b,c,d)\n");
  scanf("%c",&a);

  switch(a)
  {
    case 'a':
      printf("The character is a\n");
      break;

    case 'b':
      printf("The character is b\n");
      break;

    case 'c':
      printf("The character is c\n");
      break;

    case 'd':
      printf("The character is d\n");
      break;

    default:
      printf("Invalid Input\n");
  }

  return 0;
}

Learn Switch Statement
Try this
How to make a calculator using switch
C Codes