The switch-case control structure:
It is a control structure used to make a decision from
number of choices.
Keywords switch, case and default are used in defining
switch-case control structure.
The switch-case control structure has the switch statement
with an expression and number of case definitions with their constants.
Only integer and single character constants can be used as
case constants.
Execution starts from the case whose constant matches with
the value of switch expression.
If the value of switch expression doesn’t match with any of
the case constants then the default case is executed.
break; statement is used to execute only a single case out
of the total structure.
Example1:
#include<stdio.h>
void main()
{
int x=20;
switch(x+10)
{
case 20:
printf("One");
case 30:
printf("\nTwo");
case 40:
printf("\nThree");
default:
printf("\nFour");
}
}
Output:
Two
Three
Four
Example explained:
The value of expression x+10 matches with case 30: hence
case 30:, 40: and default cases are executed.
Example2:
#include<stdio.h>
void main()
{
int x=20;
switch(x+10)
{
case 20:
printf("One");
break;
case 30:
printf("\nTwo");
break;
case 40:
printf("\nThree");
break;
default:
printf("\nFour");
}
}
Output:
Two
Example explained:
Because of break; statement only case 30: is executed.
Specification1:
Accept any two numbers and print whether the sum,
subtraction, product or division by taking the choice from user.
Program:
#include<stdio.h>
void main()
{
int x,y,ch;
printf("Enter two numbers:\n");
scanf("%d%d",&x,&y);
printf("1.Addition\n
2.Substraction\n3.Product\n4.Division\nEnter your choice 1..4:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Addition
%d",x+y);
break;
case 2:
printf("Subtraction
%d",x-y);
break;
case 3:
printf("Product
%d",x*y);
break;
case 4:
printf("Division
%f",(float)x/y);
break;
default:
printf("Invalid
choice");
}
}
Execution1:
Enter two numbers:
7 2
1.Addition
2.Substraction
3.Product
4.Division
Enter your choice 1..4: 4
Division 3.500000
Execution2:
Enter two numbers:
7 2
1.Addition
2.Substraction
3.Product
4.Division
Enter your choice 1..4: 8
Invalid choice
Specification2:
Accept any alphabet and print whether it is a vowel or
consonant.
Program:
#include<stdio.h>
void main()
{
char ch;
printf("Enter any alphabet:");
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("Vowel");
break;
default:
printf("Consonant");
}
}
</code></pre>
Execution1:
Enter any alphabet: e
Vowel
Execution2:
Enter any alphabet: P
Consonant
Specification3:
Accept any number and print whether the number is an even or
odd
|
#include<stdio.h>
else
void main()
printf("Odd Number");
{
int n;
printf("Enter any number");
scanf("%d",&n);
switch(n%2)
{
case 0:
printf("Even Number");
break;
case 1:
printf("Odd Number");
break;
}
}
Execution1:
Enter any number: 25
Odd number
Execution2:
Enter any number: 12
Even number
Specification4:
Accept two numbers and print the biggest number.
Program:
<pre><code>
#include
void main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
switch(a==b)
{
case 1:
printf("Equals");
break;
case 0:
switch(a>b)
{
case 1:
printf("Biggest number %d",a);
break;
case 0:
printf("Biggest number %d",b);
}
}
}
Execution1:
Enter two numbers:
10 10
Equals
Execution2:
Enter two numbers:
30 10
Biggest number 30
Specification5:
Accept any three digit number and print it in words.
Program:
<pre><code>
#include<stdio.h>
void main()
{
int n,first,second,third;
printf("Enter any three digit number:");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
/* printing the hundreds */
switch(first)
{
case 9:
printf("Nine
hundred");
break;
case 8:
printf("Eight
hundred");
break;
case 7:
printf("Seven hundred");
break;
case 6:
printf("Six
hundred");
break;
case 5:
printf("Five
hundred");
break;
case 4:
printf("Four
hundred");
break;
case 3:
printf("Three
hundred");
break;
case 2:
printf("Two
hundred");
break;
case 1:
printf("One
hundred");
break;
}
if(second!=0 || third!=0)
printf( "and ");
/* printing tens */
switch(second)
{
case 9:
printf("
Ninety ");
break;
case 8:
printf(" Eighty
");
break;
case 7:
printf("
Seventy ");
break;
case 6:
printf("
Sixty ");
break;
case 5:
printf("
Fifty ");
break;
case 4:
printf(" Forty ");
break;
case 3:
printf("
Thirty ");
break;
case 2:
printf("
Twenty ");
break;
case 1:
switch(third)
{
case
0:
printf(" Ten ");
break;
case
1:
printf(" Eleven ");
break;
case
2:
printf(" Twelve ");
break;
case
3:
printf(" Thirteen ");
break;
case
4:
printf(" Fourteen ");
break;
case
5:
printf(" Fifteen ");
break;
case 6:
printf(" Sixteen ");
break;
case
7:
printf("Seventeen ");
break;
case
8:
printf(" Eighteen ");
break;
case
9:
printf(" Nineteen ");
break;
} /*end of inner switch*/
}/*end of outer switch*/
/* printing ones */
if(second!=1)
{
switch(third)
{
case 9:
printf("Nine");
break;
case 8:
printf("Eight");
break;
case 7:
printf("Seven");
break;
case 6:
printf("Six");
break;
case 5:
printf("Five");
break;
case 4:
printf("Four");
break;
case 3:
printf("Three");
break;
case 2:
printf("Two");
break;
case 1:
printf("One");
break;
} /*end of
switch*/
}/*end of if*/
}
Execution1:
Enter any three digit number: 345
Three hundred and Forty Five
Execution2:
Enter any three digit number: 415
Four hundred and Fifteen
Execution3:
Enter any three digit number: 400
Four hundred
Execution4:
Enter any three digit number: 405
Four hundred and Five
Points to remember:
Floating point and string constants can’t be used as case
constants.
Example:
<pre><code>
#include<stdio.h>
void main()
{
float n=12.25;
switch(n)
{
case 10.75:
printf("one");
break;
case 12.25:
printf("two");
break;
case 15.50:
printf("three");
break;
default:
printf("
four");
}
}
Output:
Error: constant expression required in function main
0 comments:
Post a Comment