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:
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
switch(expression) {
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
statement(s);
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:
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.