The statements which are used to execute only specific block of statements in a given series of block are called case control statements.
There are 4 types of case statements in C language. They are,
- switch case
- break
- continue
- goto
1. switch case statement:
This is used to execute only specific case statements based on the switch expression.
Syntax :
switch (expression)
{
case label1:
statements;
break;
case label2:
statements;
break;
default:
statements;
break;
}
Example program for switch..case:
#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:
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:
#include <stdio.h>
int main()
{
int i;
for(i=0;i
{
if(i==5)
{
printf("Coming out of for loop when i = 5");
break;
}
printf("%d\n",i);
}
}
Output:
01234 Coming out of for loop when i = 5
3. Continue statement:
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:
#include <stdio.h>
int main()
{
int i;
for(i=0;i
{
if(i==5 || i==6)
{
printf("Skipping %d from display using " \
"continue statement \n",i);
continue;
}
printf("%d\n",i);
}
}
Output:
01234 Skipping 5 from display using continue statement Skipping 6 from display using continue statement 789
4. goto statements:
goto statements is used to transfer the normal flow of a program to the specified label in the program.
Syntax:
{
…….
go to label;
…….
…….
Label:
Statements;
}
Example program for goto:
#include <stdio.h>
int main()
{
int i;
for(i=0;i
{
if(i==5)
{
printf(" We are using goto statement when i = 5 \n");
goto HAI;
}
printf("%d\n",i);
}
HAI : printf("Now, we are inside label name hai \n");
}
Output:
01234 We are using goto statement when i = 5
No comments:
Post a Comment