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
0~39 BAD
40~69 Poor
70~89 Good
90~100 Excellent!!
Printable View
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
0~39 BAD
40~69 Poor
70~89 Good
90~100 Excellent!!
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};
Yup nested if-else is better but i desire to know the "switch" way
>>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";
}
oh, i see. Thanks again salem and fellows..!!