0% found this document useful (0 votes)
2 views4 pages

C break statement

The document explains the use of the break statement in C, which exits loops and switch statements, and provides examples of its implementation. It also describes the switch statement, its syntax, rules, and provides a program example. Additionally, it covers the exit() function, which terminates a program, highlighting the difference between break and exit().

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

C break statement

The document explains the use of the break statement in C, which exits loops and switch statements, and provides examples of its implementation. It also describes the switch statement, its syntax, rules, and provides a program example. Additionally, it covers the exit() function, which terminates a program, highlighting the difference between break and exit().

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

Program of Break Keyword


int main ()
{
int a = 10;
while(a<20 )
{
printf("value of a: %d\n",a);
a++;
if(a>15)
{
break;
}
}
}
Program of Break keyword using For loop
int main ()
{
int a;
for(a=1;a<=20;a++)
{
printf("value of a: %d\n",a);
if(a>15)
{
break;
}
}
}

C Switch statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called
a case, and the variable being switched on is checked for each switch case

Syntax

The syntax for a switch statement in C programming language is as follows −a

switch(expression) {

case constant-expression :

statement(s);

break;

case constant-expression :

statement(s);

break;

statement(s);

Rules for using switch statement

1. The expression (after switch keyword) must yield an integer value i.e. the expression should be
an integer or a variable or an expression that evaluates to an integer.
2. The case label values must be unique.
3. The case label must end with a colon (:)
Programs For Switch & break Keyword
#include<stdio.h>
int main ()
{
char G = 'D';
switch(G)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
printf("Good!\n" );
break;
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid G\n" );
}
printf("Your G is %c\n", G );
return 0;
}

C exit() function:

In C exit() is a predefined/ library function used to terminate the currently running


program(process) immediately.
Syntax: void exit(int status);
status -> The status in an integer value returned to the parent process.
Here 0 usually means program completed successfully, and nonzero values are used as error
codes. e.g exit(0);

Programs for exit()


#include <stdio.h>
#include <stdlib.h>
int main()
{
int pics;
printf("number of pics clicked : ");
scanf("%d",&pics);
if(pics<=5)
goto sos;
else
{
printf("Selfie Person");
exit(0);
}
sos:
printf("Hate Photos");
}

The major difference between break and exit () is that break is a keyword, which causes an
immediate exit from the switch or loop (for, while or do), while exit() is a standard library
function, which terminates program execution when it is called.

You might also like