switch statement
General form:
switch(expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
........
........
default:
default-block
break;
}
statement-x;
1.
#include<stdio.h>
#incude<conio.h>
int main()
{
int a;
printf("Enter the value:");
scanf("%d",&a);
switch(a)
{
case '1':
printf("GOOD");
break;
case '2':
printf("VERY GOOD");
break;
case '3':
printf("VERY VERY GOOD");
break;
}
return 0;
}
Output:
Enter the value:3
VERY VERY GOOD
Other output:
Enter the value:1
GOOD
2.
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("Enter the value:");
scanf("%d",&a);
switch(a)
{
case '1':
case '2':
printf("GOOD");
break;
case '3':
printf("VERY GOOD");
break;
}
return 0;
}
Output:
Enter the value:1
GOOD
Other output:
Enter the value:2
GOOD
Other output:
Enter the value:3
VERY GOOD
Check vowel
3.
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
/*if ch matches any case then it prints & breaks the execution */
default:
printf("%c is not a vowel.\n", ch);
/*if the ch is not from the cases then prints ch is not a vowel */
}
return 0;
}
General form:
switch(expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
........
........
default:
default-block
break;
}
statement-x;
1.
#include<stdio.h>
#incude<conio.h>
int main()
{
int a;
printf("Enter the value:");
scanf("%d",&a);
switch(a)
{
case '1':
printf("GOOD");
break;
case '2':
printf("VERY GOOD");
break;
case '3':
printf("VERY VERY GOOD");
break;
}
return 0;
}
Output:
Enter the value:3
VERY VERY GOOD
Other output:
Enter the value:1
GOOD
2.
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("Enter the value:");
scanf("%d",&a);
switch(a)
{
case '1':
case '2':
printf("GOOD");
break;
case '3':
printf("VERY GOOD");
break;
}
return 0;
}
Output:
Enter the value:1
GOOD
Other output:
Enter the value:2
GOOD
Other output:
Enter the value:3
VERY GOOD
Check vowel
3.
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
/*if ch matches any case then it prints & breaks the execution */
default:
printf("%c is not a vowel.\n", ch);
/*if the ch is not from the cases then prints ch is not a vowel */
}
return 0;
}
Output:
Input a character
e
e is a vowel.

No comments:
Post a Comment