Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:18:30+00:00 2026-05-23T02:18:30+00:00

Quick question, I’m writing a Grade letter program using only switch statements. With if

  • 0

Quick question, I’m writing a Grade letter program using only switch statements. With if statements I can easily use <, or > to designate a certain range. If the user inputs a number such as 93 I would have to list all the cases from 99-90 which is too redundant. I already wrote the program and it works fine, but I want to get away from bad code. Is there a more reasonable way to write this without listing multiple cases? Hope this makes sense…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:18:31+00:00Added an answer on May 23, 2026 at 2:18 am

    You wouldn’t want to use a switch statement to determine where a value falls in a set of ranges.

    If you want to generalize a bit, and avoid multiple if/else statements with hard coded conditions, you could create a map. i.e. you are essentially mapping a set of integers to a set of grades.

    Below is a simple example that uses a table lookup to map scores to grades. You could also create a closed mathematical formula that maps scores to grades, and just compute the grade directly from the score, instead of looking it up via a table. e.g. f(x) = floor(2*(x-50)/20) would map scores from [0,100] into integer grades in the range [0, 5]

    typedef struct
    {
        int rangeLow;
        char grade;
    } RangeToGradeMap;
    
    RangeToGradeMap[] scoreToGradeMapping =
    {
        { 90, 'A' }
        { 85, 'B' },
        { 70, 'C' },
        { 60, 'D' },
        { 0,  'F' },
    };
    
    char GradeLookup(int score)
    {
        size_t N = sizeof(scoreToGradeMapping)/sizeof(RangeToGradeMap);
    
        for (size_t i = 0; i < N; ++i)
        {
            if (score >= scoreToGradeMapping[i].rangeLow)
            {
                return scoreToGradeMapping[i].grade;
            }
        }
    
        // If we got here, there was an error - we didn't find the range
    
        return '?';
    }
    
    int main(int argc, char** argv)
    {
        char grade = GradeLookup(87);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question: can I use a JavaScript library (i.e. jQuery) in developing a Mac
quick question Can you use the free() function without having to prior call a
Quick question: How can I use good, old jQuery Validate to validate a form
Quick question. What do you think, I have a few sites that use a
Quick question about SP 2010 licensing. Is it totally free? Can I install onto
Quick question, I'm using the Visual Studio's testing framework for unit testing. Just wondering
Quick question: I'd like to hear your thoughts on when to use State versus
Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I'm
Quick question: When do you decide to use properties (in C#) and when do
Quick question which I thought would be easily answered. I have a .net website,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.