Break Statement in C

C break statement

C break statement:

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. The break statement in C can be used in the following two scenarios:

  1. With switch case
  2. With loop

Syntax:

  1. //loop or switch case 
  2. break;

Flowchart of break in c

Break Statement in C

Example

#include<stdio.h>

#include<stdlib.h>

void main ()

{

int i;

for(i = 0; i<10; i++)

{

printf(“%d “,i);

if(i == 5)

break;

}

printf(“came outside of loop i = %d”,i);

 

}

Output

0 1 2 3 4 5 came outside of loop i = 5

C break statement with the nested loop

In such case, it breaks only the inner loop, but not outer loop.

#include<stdio.h>

int main(){

int i=1,j=1;//initializing a local variable  

for(i=1;i<=3;i++){

for(j=1;j<=3;j++){

printf(“%d &d\n”,i,j);

if(i==2 && j==2){

break;//will break loop of j only  

}

}//end of for loop  

return 0;

}

Output

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3

As you can see the output on the console, 2 3 is not printed because there is a break statement after printing i==2 and j==2. But 3 1, 3 2 and 3 3 are printed because the break statement is used to break the inner loop only.

break statement with while loop

Consider the following example to use break statement inside while loop.

#include<stdio.h>

void main ()

{

int i = 0;

while(1)

{

printf(“%d  “,i);

i++;

if(i == 10)

break;

}

printf(“came out of while loop”);

}

Output

0  1  2  3  4  5  6  7  8  9  came out of while loop	

break statement with do-while loop

Consider the following example to use the break statement with a do-while loop.

#include<stdio.h>

void main ()

{

int n=2,i,choice;

do

{

i=1;

while(i<=10)

{

printf(“%d X %d = %d\n”,n,i,n*i);

i++;

}

printf(“do you want to continue with the table of %d , enter any non-zero value to continue.”,n+1);

scanf(“%d”,&choice);

if(choice == 0)

{

break;

}

n++;

}while(1);

}

Output

2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
do you want to continue with the table of 3 , enter any non-zero value to continue.1
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
do you want to continue with the table of 4 , enter any non-zero value to continue.0

 

Related Post:

Features of C Programming Language

Variables in C

Data Types in C

Keywords in C

C Operators

Comments in C

Escape Sequence in C

C Functions

Storage Classes in C

Dynamic memory allocation in C

Factorial Program in C

Leap year program in C

Fibonacci Series in C

Prime Number program in C

Palindrome program in C

Sum of digits program in C

Escape Sequence in C

ASCII value in C

Difference Between Type Casting and Type Conversion in C

Difference Between Variables and Constants

Matrix multiplication in C

C Program to generate Fibonacci Triangle

C Program to print “hello” without semicolon

C Program to swap two numbers without third variable

C Program to reverse number

Count the number of digits in C

Tokens in C

C Identifiers

C Strings

Compile time vs Runtime in C

C goto statement

Type Casting in C

C String Functions

C Pointers

Dangling Pointers in C

void pointer in C

Pointer to Pointer in C

Recursion in C

Call by value and Call by reference in C

File Handling in C

C fprintf() and fscanf()

C fputc() and fgetc()

C fputs() and fgets()

C fseek() function

Constant Pointers in C