• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Switch Case and Goto Statement in C

Switch Case and Goto Statement with suitable Exapmples

Switch

switch is a keyword , by using switch we can create a selection statement with multiple choices.

  • Multiple choices can be constructed by using case keyword.
  • When we are working with the switch statement, it requires a condition or expression of type an integral value only.
  • Case keyword always require an integer constant value or an integral constant expression only.

 

syntax :-     switch (condition/exp)

{

case const1 :

Block1;

break;

case const2:

Block 2;

break;

 

case const3:

Block 3;

break;

_ _ _ _ _ _ _ _ _

– – – – – – — – – –

default;

Block-n;

 

  • When are working with switch statement, at the time of compilation, condition or expression returns value will map with case constant value.
  • At the time of execution , if matching case is available, then from that matching case, up to break, everything will be executed, if break is not available then including default all the cases are executed
  • At the time of execution , if matching case is not available, then control will pass to default block.
  • Default is a special kind of case which will executed automatically when the matching case is not available.
  • Using default is always optional, It is recommended to use when we are not handling all cases of switch block.
  • By using nested if else also, it is possible to create multiple blocks, but if we require to create “n” no. of blocks, then n-1 conditions are mandatory where as in switch, multiple blocks under single condition.
  • When we are working nested if else, at even any point of time only one block can be executed, but in switch it is possible to execute more than one block by removing break statement between the blocks

 

  • void main()

{

int i;

i=2;

switch(2)

{

case1: printf(“A”);

case2: printf(“B”);

case3:printf(“c”);

default: printf(“D”);

}

}

 o/p: BCD

 

 

  • void main()

{

int i;

i=1;

switch(i)

{

case 1: printf(“A”);

case2: printf(“B”);

break;

case3:   printf(“c”);

break;

default: printf(“D”);

}

}

o/p : AB

 

 

  • void main()

{

int i;

i=3;

switch(i)

{

case 1: printf(“1”);

case2: printf(“B”);

break;

case3:   printf(“3”);

break;

default: printf(“D”);

}

}

 

o/p : 3

 

  • void main()

{

int i;

i=5;

switch(i)

{

case 1: printf(“A”);

case2: printf(“2”);

break;

case3:   printf(“B”);

break;

default: printf(“D”);

}

}

O/p: D

 

  • void main()

{

int i;

i=3;

switch(i)

{

case 1: printf(“1”);

case2: printf(“c”);

break;

case3:   printf(“2”);

break;

default: printf(“D”);

}

}

 

o/p: c2

 

  • when we are working with the switch statements, cases can be constructed randomly i.e in any sequence we can place when we are constructed the cases randomly inorder to execute switch block from matching case upto break everything will execute in any sequence.

 

 

 

  • void main()

{

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.

 

  • void main()

{

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

 

  • void main()

{

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

 

  • void main()

{

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”);

}

}

 

 

  • void main()

{

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.

 

void main()

{

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

 

  • void main()

{

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:

 

goto is a keyword, by using goto, we can pass the control anywhere within the program

  • when we are working with the goto , always it require an identifier called label
  • Any valid identifier followed by colon is called label.
  • Goto statement is called unstructed programming statement because it breaks the rule of structured programming.

 

 

syntax :  statement 1;

statement 2;

goto LABEL;

statement 3;

LABEL:

statement 4;

statement 5;

 

 

  • void main()

{

printf(“A”);

printf(“B”);

goto ABC;

printf(“welcome”);

ABC:

print(“x”);

print(“y”);

}

o/p: ABXY

 

  • void main()

{

printf(“A”);

printf(“B”);

goto ABC;

printf(“welcome”);

printf(“Hello”)

goto xyz;

printf(“y”);

xyz:

printf (“x”);

printf(“y”);

print(“x”);

}

o/p: ABwelcomeHelloxy

 

  • In order to execute the program if the label is occurred, if it is executed automatically without calling also creating the label is always optional, after creating the lable, calling the label is also optional , but if were calling then it must be required within the program or else error will occur.
  • In implementation when we require to take the repetition without using loop then recommended to use goto statement.

 

To print even nos without a loop

 

  • void main()

{

int a;

a=2;

EVEN:

printf(“%d”,a);

a+=2;

if(a<=20)

goto EVEN;

}

o/p : 2 4 6 8 – – – – 18 20

 

  • void main()

{

printf(“NIT”);

goto XYZ;

printf(“welcome”);

ABC:

printf(“A”);

printf(“B”);

XYZ:

printf(“x”);

printf(“y”);

goto ABC;

}

o/p: NITXYABXYABXY_ _ _ _ xtrl+break;

 

 

  • void main()

{

printf(“welcome”);

printf(“NIT”);

goto ABC;

printf(“x”);

printf(“y”);

abc:

printf(“A”);

printf(“B”);

}

o/p: Error undefined label ‘ABC’

 

  • When we are working with goto labels works with the help of case sensitive, i.e uppercase and lowercase content is treated as different.

 

 

void main()

{

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

  • void main()

{

int i;

i=1;

switch(i)

{

case 1: printf(“A”);

break;

case 2: printf(“B”);

Break;

case default: printf(“D”);

}

}

o/p: error

 

  • void main()

{

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

 

  • void main()

{

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.

 

  • void main()

{

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();

}

 

void main()

{

int yy,nleap;

long int dp;

clrscr();

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.

 

Digit to text conversion:

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.

 

Summary
Review Date
Reviewed Item
Switch Case and Goto Statement in C
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Switch Case and Goto Statement in C"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.