BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C – break statement in C programming

Last Updated: September 23, 2017 by Chaitanya Singh | Filed Under: c-programming

The break statement is used inside loops and switch case.

C – break statement

1. It is used to come out of the loop instantly. 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.
2. 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).

Flow diagram of break statement

C break statement

Syntax:

break;

Example – Use of break in a while loop

#include <stdio.h>
int main()
{
     int num =0;
     while(num<=100)
     {
        printf("value of variable num is: %d\n", num);
        if (num==2)
        {
            break;
        }
        num++;
     }
     printf("Out of while-loop");
     return 0;
}

Output:

value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop

Example – Use of break in a for loop

#include <stdio.h>
int main()
{
      int var;
      for (var =100; var>=10; var --)
      {
           printf("var: %d\n", var);
           if (var==99)
           {
               break;
           }
      }
     printf("Out of for-loop");
     return 0;
}

Output:

var: 100
var: 99
Out of for-loop

Example – Use of break statement in switch-case

#include <stdio.h>
int main()
{
      int num;
      printf("Enter value of num:");
      scanf("%d",&num);
      switch (num)
      {
          case 1:
             printf("You have entered value 1\n");
             break;
          case 2:
             printf("You have entered value 2\n");
             break;
          case 3:
             printf("You have entered value 3\n");
             break;
          default:
             printf("Input value is other than 1,2 & 3 ");
     }
     return 0;
}

Output:

Enter value of num:2
You have entered value 2

You would always want to use break statement in a switch case block, otherwise once a case block is executed, the rest of the subsequent case blocks will execute. For example, if we don’t use the break statement after every case block then the output of this program would be:

Enter value of num:2
You have entered value 2
You have entered value 3
Input value is other than 1,2 & 3
❮ PreviousNext ❯

Top Related Articles:

  1. C – Strings and String functions with examples
  2. C – Pointer to Pointer (Double Pointer) with example
  3. Functions printf() and scanf() in C
  4. C – do while loop in C programming with example
  5. C Program Structure – First C Program

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Haseeb khattak says

    September 6, 2015 at 11:41 AM

    in break statement, the variable “num” is not declared in program.

    Reply
  2. sawan khare says

    September 21, 2015 at 3:56 PM

    Nice collections of examples of break statement in switch case in c programming…Thankyou

    Reply
    • judy says

      February 15, 2016 at 6:36 PM

      write a c program of switch case statement that will output the following: 2*3=6. 2+3=5. 4+6=10. 4/5=0.8

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap