switch is a keyword , by using switch we can create a selection statement with multiple choices.
syntax: switch (condition/exp) { case const1 : Block1; break; case const2: Block 2; break; case const3: Block 3; break; _ _ _ _ _ _ _ _ _ - - - - - - -- - - - default; Block-n;
{ int i; i=2; switch(2) { case1: printf(“A”); case2: printf(“B”); case3:printf(“c”); default: printf(“D”); } }
o/p: BCD
{ int i; i=1; switch(i) { case 1: printf(“A”); case2: printf(“B”); break; case3: printf(“c”); break; default: printf(“D”); } }
o/p : AB
{ int i; i=3; switch(i) { case 1: printf(“1”); case2: printf(“B”); break; case3: printf(“3”); break; default: printf(“D”); } }
o/p : 3
{ int i; i=5; switch(i) { case 1: printf(“A”); case2: printf(“2”); break; case3: printf(“B”); break; default: printf(“D”); } }
o/p: D
{ int i; i=3; switch(i) { case 1: printf(“1”); case2: printf(“c”); break; case3: printf(“2”); break; default: printf(“D”); } }
o/p: c2
{ int i; i=5; switch(i) { case 1: printf(“2”); case2: printf(“D”); break; case3: printf(“1”); break; default: printf(“C”); } }
o/p: D1
When we are working with the default it can be place anywhere within the switch body but generally recommended to place at the end of the switch body only.
{ int i; //i=4 i=4,8; switch(i) { case1: printf(“A”); break; case2: printf(“B”); break; case3: printf(“c”); break; default: printf(“D”); } }
o/p : D
{ float i;
// switch statement expression must be of i=1;
integral type. switch(i) { case 1: printf(“1”); case2: printf(“B”); break; case3: printf(“c”); break; default: printf(“D”); }
}
o/p: Error
{ int i; i=2; switch(i)
{
case 1.0: printf(“A”); // const expression is required
case2: printf(“B”); float datatype is not accepted break; in switch
case3: printf(“c”); break; default: printf(“D”); } }
{ int a,b,c,d; a=1;b=2;c=3; d=c-a; switch(d)
{ case a: printf(“1”); case ‘a’ : valid
case b: printf(“B”); break;
case c: printf(“3”); break; default: printf(“D”); }
}
o/p: const expression required.
case need to be constructed by using constant data or constant expression only.
{ int i; i=4%2; switch(i)
{ case 2+3: printf(“A”);
// case5: Break; case 5*2: printf(“B”); break; case 5-4: printf(“c”); // case 1; break; case 8%4: printf(“D”); // case 0; break; }
}
o/p: D
When we are constructing the cases by using expression format data, then mapping will be takes place according to return value of the expression
{ int i; i=5>2; switch(i) { case 2<5: printf(“A”); // case1: Break; case 3!=3: printf(“B”); // case 0; break; case 5/2: printf(“3”); // case 2; break; case 1!=2>5: printf(“D”); // case 1;
o/p: Error duplicate case.
When we are working with the switch statement, cases must be unique. i.e more than one case cannot be constructed by using same constant value, if we are constructing then, we will get an error. I.e duplicate cases.
goto is a keyword, by using goto, we can pass the control anywhere within the program
syntax : statement 1; statement 2; goto LABEL; statement 3; LABEL: statement 4; statement 5;
{ printf(“A”); printf(“B”); goto ABC; printf(“welcome”); ABC: print(“x”); print(“y”); }
o/p: ABXY
{ printf(“A”); printf(“B”); goto ABC; printf(“welcome”); printf(“Hello”) goto xyz; printf(“y”); xyz: printf (“x”); printf(“y”); print(“x”); }
o/p: ABwelcomeHelloxy
To print even nos without a loop
{ int a; a=2; EVEN: printf(“%d”,a); a+=2; if(a<=20) goto EVEN; } o/p : 2 4 6 8 - - - - 18 20
{ printf(“NIT”); goto XYZ; printf(“welcome”); ABC: printf(“A”); printf(“B”); XYZ: printf(“x”); printf(“y”); goto ABC; } o/p: NITXYABXYABXY_ _ _ _ xtrl+break;
{ printf(“welcome”); printf(“NIT”); goto ABC; printf(“x”); printf(“y”); abc: printf(“A”); printf(“B”); } o/p: Error undefined label ‘ABC’
{ int i; i=2; switch(i) { case1: printf(“A”); No space between case and constant break; case2: printf(“B”); break; case3: printf(“c”); break; default : printf(“d”); } }
o/p: D
According to the syntax of the switch, case and constant value must be required space, if space is not provided then compiler will create a label, matching case is not exist so default will execute
{ int i; i=1; switch(i) { case 1: printf(“A”); break; case 2: printf(“B”); Break; case default: printf(“D”); } }
o/p: error
{ int i; i=2; switch(i) { case 1: printf(“A”); break; case 2: printf(“B”); continue; case 3: printf(“3”); break; default: printf(“4”); } }
o/p: Error misplaced continue
{ int i; i=3; switch(i); dummy switch body { case 1: printf(“A”); break; case 2: printf(“B”); Break; case default: printf(“D”); } }
o/p:- Error misplaced break; case and default
When we are placing the semicolon at end of the switch then it became dummy switch. When dummy switch body is constructed, then compiler will creates new body without any statements and current body became outside so break; case and default became misplaced.
{ int dd, mm, yy, nleap; long int dp; clrscr();
do { printf(“\n Enter year:”); scanf(“%d”, &yy); }
while(yy<1); do { printf(“\n Enter month:”);
scanf(“%d”,&mm); } while(mm<|| mm>12);
printf(“\n Enter date:”);
scanf(“%d”, &dd); if(dd<1 || dd>31)
{ printf(“\n invalid date”); goto END; }
if ((mm=4 || mm=6 || mm=9 || mm=11) && dd>30) { printf(“\n invalid date”); goto END; hum auna vachaka malli nu nannu tedatav lada nedra moham mida ami venav anduku la ani nana l }
if(yy%4==0 && yy%100!=0 || y%400 ==0) { if(mm==2 && dd>29) { printf(“\n invalid date”); goto END; } } else { if(mm==2 && dd>28) { printf(“\n invalid date”); goto END; } }
nleap = (yy-1)/4 – (yy-1)/100 + (yy-1)/400; dp=(yy-1) * 365l+nleap; switch(mm) { case 12: dp+=30; case 11: dp+=30; case 10: dp+=30; case 9: dp+=31; case 8: dp+=31; case 7: dp+=30; case 6: dp+=31; case 5: dp+=30; case 4: dp+=31; case 3: dp+=28; case 2: dp+=31; case 1: dp+=dd; } if((yy%4==0 && yy%100!=0 && yy%400 ==0) && mm>2) ++dp; printf(“\n%2.2d%2.2d%d weekdays:”,dd,mm,yy); switch(dp%y)
{ case 1: printf (“monday”); break;
case 2: printf(“tuesday”); break;
case 3: printf(“wednesday”); break;
case 4: printf(“Thursday”); break;
case 5: printf(“friday”); break;
case 6: printf(“saturday”); break;
case 0: printf(“sunday”); }
End: getch();
}
{ int yy,
nleap;
long int dp; c
lrscr();
printf(“\n Enter a year:”); scanf(“%d”, &yy);
if(yy%4==0 && yy%100!=0 || yy%400==0) { nleap=(yy-1)/4 – (yy-1)/100 + (yy-1)/400; dp=(yy-1) * 365l+nleap; dp+=31; //jan dp+=29; // feb printf(“\n29-Feb-%d is :”,yy); switch(dp%7)
{
case1: printf(“Monday”); break;
case 2: printf(“tuesday”); break;
case 3: printf(“wednesday”); break;
case 4: printf(“Thursday”); break;
case 5: printf(“Friday”); break;
case 5: printf(“Saturday”); break;
case 0: printf(“Sunday”); } } else pritntf(“\n%d is not a leap year:”,yy) getch();
}
o/p: Enter a year : 2013 2012 is not a leap year.
void main()
{ int n,rn=0,count=0; clrscr(); printf(“\n Enter a value:”);
scanf(“%d”, &n); while(n) { m=m*10+n%10; ++count; n=n/10; }
while(rn) { switch(m%10)
{
case 0: printf(“Zero”); break;
case 1: printf(“one”); break;
case 2: printf(“two”); break;
case 3: printf (“three”); break;
case 4: printf(“four”); break;
case 5: printf(“five”); break;
case 6: printf(“six”); break;
case 7: printf(“seven”); break;
case 8: printf(“eight”); break;
case 9: printf(“Nine”); break; }
m=m/10; _ _ count; }
while(count>0) { printf(“Zero”); _ _ count; }
while (count>0) { print(“Zero”); _ _ count; }
getch(); }
o/p: Enter a value: 1249 one two four nine.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.