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 6540415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:58:35+00:00 2026-05-25T10:58:35+00:00

First, I realize that from a performance perspective, the design of this switch statement

  • 0

First, I realize that from a performance perspective, the design of this switch statement is slow because cout is being called several times in certain cases. That aside, is this style of writing a switch statement not good coding practice. In other words, would it be better to handle each case individually and break or is the fall-through better?

int main(void)
{
    int number;
    cout << "Enter a number between 1 and 10 and I will display its Roman numeral equivalent." << endl
         << "> ";
    cin >> number;

    cout << "Roman numeral: ";
    switch (number)
    {
        case 3:
            cout << "I";
        case 2:
            cout << "I";
        case 1:
            cout << "I";
            break;
        case 4:
            cout << "I";
        case 5:
            cout << "V";
            break;
        case 6:
            cout << "VI";
            break;
        case 7:
            cout << "VII";
            break;
        case 8:
            cout << "VIII";
            break;
        case 9:
            cout << "I";
        case 10:
            cout << "X";
            break;
        default:
            cout << "Error!\nYou did not enter a number between 1 and 10";
    }

    cout << endl;

    return 0;
}
  • 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-25T10:58:35+00:00Added an answer on May 25, 2026 at 10:58 am

    Switch statements aren’t slow, they’re usually optimised to jump-tables by the compiler. And if you’re sure that switch statement works as expected, it’s fine and is a pretty cool way of doing it.

    That said, you’d have the same number of cases if you handled each one individually. If that’s all you’re doing, I’d probably change it to handle each one seperately and not do fall through. It’s more comprehendable and easily maintained that way:

    switch (number)
    {
        case 1:
            cout << "I";
            break;
        case 2:
            cout << "II";
            break;
        case 3:
            cout << "III";
            break;
        case 4:
            cout << "IV";
            break;
        case 5:
            cout << "V";
            break;
        case 6:
            cout << "VI";
            break;
        case 7:
            cout << "VII";
            break;
        case 8:
            cout << "VIII";
            break;
        case 9:
            cout << "IX";
            break;
        case 10:
            cout << "X";
            break;
        default:
            cout << "Error!\nYou did not enter a number between 1 and 10";
    }
    

    And like @paxdiablo suggested, to enhance readability you can put the case, statement, and break all on the same line if it looks better to you:

    case 1: cout << "I";  break;
    case 2: cout << "II"; break;
    // etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This an excerpt from my assembly program First: dw 0xaabbccdd Now I realize that
First let me say that I really feel directionless on this question. I am
If I have something like this: x = from f in first.Include(second) where f.id
First off, I realize that there is no such thing as a perfectly secure
I realize that: sudo -i env is different from: sudo -i sudo env They
For moving an individual from one branch to another I realize that there are
I realize that this question might not make sense to some, but I was
First let me say I come from a background in Flash/AS3, which I realize
First off, there's a bit of background to this issue available on my blog:
First off, I'm working on an app that's written such that some of your

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.