The statements which are used to execute only specific block of statements in a series of blocks are called case control statements.
There are 4 types of case control statements in C language. They are,
- switch
- break
- continue
- goto
……
1. switch case statement in C:
- Switch case statements are used to execute only specific case statements based on the switch expression.
- Below is the syntax for switch case statement.
switch (expression)
{
case label1: statements;
break;
case label2: statements;
break;
default: statements;
break;
}
Example program for switch..case statement in C:
#include <stdio.h>
int main ()
{
int value = 3;
switch(value)
{
case 1:
printf("Value is 1 n" );
break;
case 2:
printf("Value is 2 n" );
break;
case 3:
printf("Value is 3 n" );
break;
case 4:
printf("Value is 4 n" );
break;
default :
printf("Value is other than 1,2,3,4 n" );
}
return 0;
}
Output:
|
Value is 3
|
2. break statement in C:
- Break statement is used to terminate the while loops, switch case loops and for loops from the subsequent execution.
- Syntax: break;
Example program for break statement in C:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("nComing out of for loop when i = 5");
break;
}
printf("%d ",i);
}
}
Output:
|
0 1 2 3 4
Coming out of for loop when i = 5 |
3. Continue statement in C:
- Continue statement is used to continue the next iteration of for loop, while loop and do-while loops. So, the remaining statements are skipped within the loop for that particular iteration.
- Syntax : continue;
Example program for continue statement in C:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5 || i==6)
{
printf("nSkipping %d from display using "
"continue statement n",i);
continue;
}
printf("%d ",i);
}
}
Output:
|
0 1 2 3 4
Skipping 5 from display using continue statement
Skipping 6 from display using continue statement 7 8 9 |
4. goto statement in C:
- goto statements is used to transfer the normal flow of a program to the specified label in the program.
- Below is the syntax for goto statement in C.
{
…….
go to label;
…….
…….
LABEL:
statements;
}
Example program for goto statement in C:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("nWe are using goto statement when i = 5");
goto HAI;
}
printf("%d ",i);
}
HAI : printf("nNow, we are inside label name "hai" n");
}
Output:
|
0 1 2 3 4
We are using goto statement when i = 5 Now, we are inside label name “hai” |
Prev Next
.