SWITCH CASE STATEMENT
The switch case statement is a special
decision maker that tests whether an
expression matches one of the numbers
of signed constant or character constant
values, with expression given inside the
braces accordingly. It can only be used
to test for equality of an expression. We
cannot use a relational or logical
expression like in if……….else
statement. Switch expression must
evaluate to an integer or character
constant value. No two case constants
can be same.
SYNTAX
Switch(expression)
{
case constant 1;
statement 1;
Break;
.
.
case constant n;
statement n;
Break;
Default:
Statement n+1
}
FLOWCHART
WAP to input number and display day such as if input 1 for Sunday, 7 for Saturday.
[using switch case statement.]
/* the c program displaying day using switch case statement.*/
#include<stdio.h>
#include<conio.h>
Void main()
{
Int choice;
clrscr();
printf(“enter the number of day”);
scanf(“%d”,&choice);
switch(choice)
{
Case 1:
Printf(“the day you’ve chosen is Sunday”);
break;
Case 2:
Printf(“the day you’ve chosen is Monday”);
break;
Case 3:
Printf(“the day you’ve chosen is Tuesday”);
break;
Case 4:
printf(“the day you’ve chosen is Wednesday”);
break;
Case 5:
printf(“the day you’ve chosen is Thursday”);
break;
Case 6:
printf(“the day you’ve chosen is Friday”);
break;
Case 7:
printf(“the day you’ve chosen is Saturday”);
break;
Default :
printf(“invalid option given”);
}
getch( );
}
WAP WHICH READS ANY TWO INTEGER VALUES FROM USER AND CALCULATES
SUM, DIFFERENCE AND PRODUCT USING SWITCH STATEMENT.
#include<stdio.h>
Void main()
{
Int a,,d,c,ch;
Printf(“enter two numbers:”;);
Scanf(“%d %d”,&a,&b);
Printf(“n 1. sum”);
Printf(“n 2 difference”);
Printf(“n 3 product”);
Printf(“n enter your choice (1-3)”);
Scanf(“%d”,&ch);
Switch(ch)
{
Case 1:
C=a+b}
}

Switch case

  • 2.
    SWITCH CASE STATEMENT Theswitch case statement is a special decision maker that tests whether an expression matches one of the numbers of signed constant or character constant values, with expression given inside the braces accordingly. It can only be used to test for equality of an expression. We cannot use a relational or logical expression like in if……….else statement. Switch expression must evaluate to an integer or character constant value. No two case constants can be same.
  • 3.
    SYNTAX Switch(expression) { case constant 1; statement1; Break; . . case constant n; statement n; Break; Default: Statement n+1 } FLOWCHART
  • 4.
    WAP to inputnumber and display day such as if input 1 for Sunday, 7 for Saturday. [using switch case statement.] /* the c program displaying day using switch case statement.*/ #include<stdio.h> #include<conio.h> Void main() { Int choice; clrscr(); printf(“enter the number of day”); scanf(“%d”,&choice); switch(choice) { Case 1: Printf(“the day you’ve chosen is Sunday”); break; Case 2: Printf(“the day you’ve chosen is Monday”); break; Case 3: Printf(“the day you’ve chosen is Tuesday”); break;
  • 5.
    Case 4: printf(“the dayyou’ve chosen is Wednesday”); break; Case 5: printf(“the day you’ve chosen is Thursday”); break; Case 6: printf(“the day you’ve chosen is Friday”); break; Case 7: printf(“the day you’ve chosen is Saturday”); break; Default : printf(“invalid option given”); } getch( ); }
  • 6.
    WAP WHICH READSANY TWO INTEGER VALUES FROM USER AND CALCULATES SUM, DIFFERENCE AND PRODUCT USING SWITCH STATEMENT. #include<stdio.h> Void main() { Int a,,d,c,ch; Printf(“enter two numbers:”;); Scanf(“%d %d”,&a,&b); Printf(“n 1. sum”); Printf(“n 2 difference”); Printf(“n 3 product”); Printf(“n enter your choice (1-3)”); Scanf(“%d”,&ch); Switch(ch) { Case 1: C=a+b} }