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

This is a discussion on How to use "switch" to perform large numbers cases? within the C Programming forums, part of the General Programming Boards category; How to use switch to make following output?( Issit required me to defined each cases from 0,1,2,3...39=BAD ...... 100 ) ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    Unhappy 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
    0~39 BAD
    40~69 Poor
    70~89 Good
    90~100 Excellent!!

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    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};
    Last edited by stumon; 07-09-2003 at 11:24 AM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    Yup nested if-else is better but i desire to know the "switch" way

  4. #4
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>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";
    }

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    Lightbulb

    oh, i see. Thanks again salem and fellows..!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  2. Very large floating point numbers
    By bulsquare in forum C Programming
    Replies: 0
    Last Post: 04-08-2002, 05:10 PM
  3. printing large decimal numbers
    By Isometric in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 09:55 PM
  4. commas in large numbers
    By HKR in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 07:08 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21