Break Statement in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

The break, continue, goto, and return statements in C are Jump control statements. These statements cause the control to pass to any desired location in the program. There are several uses of break statement in C.

Break keyword in C

When the break keyword is encountered inside the loop then immediately the loop is terminated and control resumes at the next statement following the loop.

Syntax:- break;

Using break inside while loop

 #include<stdio.h>
 int main()
 {
     int i=1;
     while(i<=10)
     {
       printf("%d\t",i);
       if(i==5)
       {
         printf("\nWhile loop terminated.");
         break;
       }
       i++;
     }
     printf("\nValue of i = %d",i);
     return 0;
 }

Output:-

1 2 3 4 5
While loop terminated.
Value of i = 5

When the value of the variable i becomes 5 then the if-condition becomes true, and the if-block is executed. The break statement is placed inside if block. Due to the break keyword, loop terminated and remaining statements outside the loop are executed.

Using break inside for loop

 #include<stdio.h>
 int main()
 {
     int i;
     for(i=1; i<=10; i++)
     {
       printf("%d\t",i);
       if(i==5)
       {
         printf("\nFor loop terminated.");
         break;
       }
     }
     printf("\nValue of i = %d",i);
     return 0;
 }

Output:-

1 2 3 4 5
For loop terminated.
Value of i = 5

Using a break in an infinite loop

In infinite loops, the break statement is used to make it a finite loop. The given below program is an infinite loop.

 #include<stdio.h>
 int main()
 {
     int i=1;
     while (1) {
       printf("%d\t",i);
       i++;
     }
     return 0;
 }

To make the above program finite, we need to use a break statement.

 #include<stdio.h>
 int main()
 {
     int i=1;
     while (1) {
       printf("%d\t",i);
       if(i==9) break;
       i++;
     }
     return 0;
 }

Output:-

1 2 3 4 5 6 7 8 9

Now, when the value of the variable i becomes 9 then if block is executed and due to break keyword loop is terminated.

 #include<stdio.h>
 int main()
 {
     int i=1;
     for(; ;) {
       printf("%d\t",i);
       if(i==9) break;
       i++;
     }
     return 0;
 }

Output:-

1 2 3 4 5 6 7 8 9

break statement

Using Break inside Nested loops

In nested loops, if a break is used inside the inner loop then it only terminates the inner loop, and if the break is placed in the outer loop then it terminates the whole nested loop.

 #include<stdio.h>
 int main()
 {
     for(int i=1; i<5; i++)
     {
       for(int j=1; j<5; j++)
       {
         if(j == 3) break;
         printf("%d \t %d\n",i, j);
       }
     }
     return 0;
 }

Output:-

1 1
1 2
2 1
2 2
3 1
3 2
4 1
4 2

In this program, the break statement is used inside the inner loop so only the inner loop is terminated.

 #include<stdio.h>
 int main()
 {
     for(int i=1; i<5; i++)
     {
       if(i == 3) break;
       for(int j=1; j<5; j++)
       {
         printf("%d \t %d\n",i, j);
       }
     }
     return 0;
 }

Output:-

1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4

Rule for break keyword in C

It should be used only with a loop or switch-case statement.

 #include<stdio.h>
 int main()
 {
     printf("Good morning");
     break;
     printf("Good evening");
     return 0;
 }

error: break statement, not within loop or switch

Use of break statement in C

The break statement has two uses.

  1. It is used to terminate a case in the switch statement.
  2. It is also used to force immediate termination of a loop, bypassing the normal loop conditional test.

Program:- Write a C program to find the sum of the given number and the program accepts input values from the user repeatedly until the user inputs 0.

 #include<stdio.h>
 int main()
 {
   int n, sum=0;
   while (1) {
     printf("Enter a number (To terminate enter 0): ");
     scanf("%d",&n);
     if(n==0) break;
     sum = sum + n;
   }
   printf("Sum = %d",sum);
   return 0;
 }

Output:-

Enter a number (To terminate enter 0): 10
Enter a number (To terminate enter 0): 92
Enter a number (To terminate enter 0): -50
Enter a number (To terminate enter 0): -15
Enter a number (To terminate enter 0): 0
Sum = 37

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

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