In this article break statement in c we give the information about  When a break statement is found in the loop, the loop closes immediately and program goes to the next statement after the loop.

break statement in c

Unconditional statements in C:

Unconditional statements such as:

Break, continue, exit and goto.

Break statement in c:

  • In switch statement we break after each case statement.
  • If we do not do this then the compiler also executes the case after the matched case.
  • If break is used then the code of that case will be executed because after case we have used break statement.
  • The compiler understands here that the sequence has to be broken here, which means it does not go to the next case.
  • That’s why break statement is used to break the sequence in switch case and loop also.
  • In other words, break statement can be used to terminate any cases in switch statement.
  • When break statement is used in a loop, then that loop ends immediately and the control goes to the next loop.
  • In this way break statement is used to terminate the sequence.

Syntax:

break;

Example:

void main ()

{

int a = 10;

while( a < 20 )

{

printf(“value of a: %d\n”, a);

a++;

if( a > 13)

{

break;

}

}

}

Output:-

value of a: 10

value of a: 11

value of a: 12

value of a: 13

Continue statements in C: 

Sometimes we have to continue the loop again from the next iteration of the loop by skipping some of the statements inside the loop when a specific condition is true in our programs, and for this we use the continue statement.

In other words Continue Statement is a loop control statement that we use in C language when we want to run the program from next iteration by skipping a particular line or code when a particular condition becomes true.

Continue statement is slightly opposite to the break statement; it does not take the control of the program out of the entire loop, but takes the control of the program to the beginning of that loop and runs the loop from the next iteration.

Syntax:-

continue;

Example:

void main ()

{

int a = 10;

do

{

if( a == 15)

{

a = a + 1;   continue;

}

printf(“value of a: %d\n”, a);

a++;

} while( a < 18 );

}

OutPut:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 16

value of a: 17

Exit statements in C:

Exit Statement:

exit() is a standard library function, which terminates program execution when it is called.

Syntax:-

exit(0);

In this program, we have done the work of multiplying two numbers. In the program you can see that we have used a while loop. Inside which we have entered the length and breadth from the user and printed the area of ​​rectangle. But we can see this loop whose condition we have given such a condition that it will always be true.

Due to which the loop will never stop. But we have used the exit function inside the loop, due to which this loop will end at once and the program will also end along with it. This is the advantage of using the exit function.

#include< stdio.h >

#include< conio.h >

#include<stdlib.h>

void main()

{

int l, b, area;

clrscr();

while (1)

{

printf(“\n Enter the length:”);

scanf(“%d”,&l);

printf(“\n Enter the breadth:”);

scanf(“%d”,&b);

area=l*b;

printf(“\n Area of rectangle :%d”,area);

exit(0);

}

Output:

Enter the length: 10

Enter the breadth: 7

Area of rectangle : 70

Goto statements in C:

goto statement:

In C language goto statement is known as jump statement because goto statement is used to transfer the control of the program to a previously defined label. The goto statement can also be used to repeat part of a program until certain conditions are met.

It can also be used to break out of multiple loop statements at once which cannot be done using a single break statement.

Syntax 1:

goto label_name;

….

label_name:

Syntax 2:

label_name:

goto label_name;

Example

/*C program to check if a number is  even or not using goto statement */

#include <stdio.h>

#include<conio.h>

void main()

{

int num ;

clrscr();

printf(“\n\n Enter the number: ”);

scanf(“%d”,&num);

if (num % 2 == 0)

goto even;  // jump to even

else

goto odd;    // jump to odd

even:

printf(“%d is even number.”, num);

odd:

printf(“%d is odd number.”, num);

getch();

}

Output: 

Enter the number: 10

10 is even number.

/* C program to print numbers from 10 to 1 using goto statement */

#include <stdio.h>

#include<conio.h>

void main()

{

int n = 10;

clrscr();

label:

printf(“%d\n”,n);

n–;

if (n >= 1)

goto label;

getch();

}

Output:

10

9

8

7

6

5

4

3

2

1

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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