开发者

how to check for multiple switch statement in c#

开发者 https://www.devze.com 2022-12-28 13:49 出处:网络
i need to check more than one statement in switch statement to evalute like int a=5; switch(a) { case 4,5:

i need to check more than one statement in switch statement to evalute like

 int a=5; 
 switch(a) 
 { 
     case 4,5:
         console.wr开发者_如何学Cite("its from 4 to 5);
         break; 
 }


You want to do:

case 4:
case 5:
//Code goes here.
break;

Remember though C# doesn't allow fall through so you can't do:

    case 4:
    //Do some stuff here
    //fall through to 5
    case 5:
    //Code goes here.
    break;


In c# you stack cases to do this:

case 4:
case 5:
   //do something
   break;
case 6:
   //do something

etc.


This allows you to execute multiple cases for 1 value.

int a=5; 
 switch(a) 
 { 
     case 4:
        // Do work here
        goto case 5;
     case 5:
         console.write("its from 4 to 5);
         break; 
 }

or

This is giving a case two labels.

 switch(a) 
 { 
     case 4:
     case 5:
         console.write("its from 4 to 5);
         break; 
 }


This is how..

 int a=5; 
 switch(a) 
 { 
     case 4:
     case 5:
         console.write("its from 4 to 5);
         break; 
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消