Introduction to Programming - (C Language) - Unit : 2 - Break & Continue Keywords

 

C break statement

The break statement in C language is used to break the execution of loop (while, do while and for) and switch case.

In case of inner loops, it terminates the control of inner loop only.

There can be two usage of C break keyword:

  1. With switch case
  2. With loop

Syntax:

jump-statement;  

break;  

The jump statement in c break syntax can be while loop, do while loop, for loop or switch case.

Flowchart of break in c



 

Example of C break statement with loop

#include <stdio.h>    

#include <conio.h>    

void main()

{    

int i=1;//initializing a local variable  

clrscr();    

//starting a loop from 1 to 10  

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

{    

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

if(i==5)

{

//if value of i is equal to 5, it will break the loop  

break;  

}  

}//end of for loop  

getch();    

}  

 

Output

1
2
3
4
5

As you can see on console output, loop from 1 to 10 is not printed after i==5.

C break statement with inner loop

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

 

#include <stdio.h>    

#include <conio.h>    

void main()

{    

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

clrscr();    

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  

getch();    

 

Output

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

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

C continue statement

The continue statement in C language is used to continue the execution of loop (while, do while and for). It is used with if condition within the loop.

In case of inner loops, it continues the control of inner loop only.

 

Syntax:

jump-statement;  

continue;  

The jump statement can be while, do while and for loop.

Example of continue statement in c

#include <stdio.h>    

#include <conio.h>    

void main()

{    

int i=1;//initializing a local variable  

clrscr();    

//starting a loop from 1 to 10  

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

{    

if(i==5)

{

//if value of i is equal to 5, it will continue the loop  

continue;  

}  

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

}//end of for loop  

getch();    

 

Output

1
2
3
4
6
7
8
9
10

As you can see, 5 is not printed on the console because loop is continued at i==5.

C continue statement with inner loop

In such case, C continue statement continues only inner loop, but not outer loop.

#include <stdio.h>    

#include <conio.h>    

void main()

{    

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

clrscr();    

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

{    

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

{  

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

{  

continue;//will continue loop of j only  

}  

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

}  

}//end of for loop  

getch();    

}    

 

Output

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

As you can see, 2 2 is not printed on the console because inner loop is continued at i==2 and j==2.

Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC