# How to use "switch" to perform large numbers cases?

• 07-09-2003
megablue
How to use "switch" to perform large numbers cases?
How to use switch to make following output?( Issit required me to defined each cases from 0,1,2,3...39=BAD ...... 100 )

Score Comment
40~69 Poor
70~89 Good
90~100 Excellent!!
• 07-09-2003
stumon
It would probably be better here to use nested If/else statements.
Code:

if (blah >= 90)
{do this};
else if (blah >= 70)
{do this};
else if (blah >= 40)
{do this};
else
{do this};

• 07-09-2003
megablue
Yup nested if-else is better but i desire to know the "switch" way
• 07-09-2003
Casey
>>Issit required me to defined each cases from 0,1,2,3...39=BAD ...... 100
Yes. A four part if/else if/else statement is just a smidge better than a 100 case switch/case statement or something weird. You can also sneak around with tables, if you really wanted to.
Code:

char *getScore(double score)
{
double  scores[] = {39.0, 69.0, 89.0, 100.0};
char  *scorestrings[] = {"BAD", "Poor", "Good", "Excellent!!"};
int    i;

for (i = 0; i < 4; i++)
{
if (score <= scores[i])
return scorestrings[i];
}

return "Invalid score";
}

• 07-09-2003
megablue
oh, i see. Thanks again salem and fellows..!!