View previous topic :: View next topic |
Author |
Message |
wynnet
Joined: 02 Dec 2003 Posts: 15
|
switch case with a range |
Posted: Wed Mar 31, 2004 4:15 pm |
|
|
How do I write a switch case with a range?
switch (number) {
case 1: tax_1() ;
break;
case 2:
case 3:
case 4: tax_2()
break;
case 10: /* a range of case 10 to case 99 */
case 11:
case 12:
..
..
case 99: tax_ 11percent()
break;
default: /* example 00 or Hex Letter */
break;
}
How do I do a shortcut instead of type case 10,case 11,..case 99? |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
|
Posted: Wed Mar 31, 2004 5:00 pm |
|
|
Don't use case, use if/else. |
|
|
random_person Guest
|
|
Posted: Wed Mar 31, 2004 5:03 pm |
|
|
i'm not sure theres a way to do that with the CCSC compiler.
it looks like in your example, you are testing for ranges...so why not just use the if/else if structure to test for ranges?
if you don't like that idea, why not put in some code before the switch that sets number = to a specific integer if it is in a given range. ie...
Code: |
if (number >=10 && number <100) number = 99;
switch(number) {...case 4:tax_2();break;case 99:tax_3;break;...}; |
if you can't change number, then use some temp variable. |
|
|
wynnet
Joined: 02 Dec 2003 Posts: 15
|
switch case with a range |
Posted: Thu Apr 01, 2004 11:47 am |
|
|
thank
I use
if (number >=10 && number <100) number = 99;
it helps me from making human typo error. |
|
|
|