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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:32:12+00:00 2026-05-24T20:32:12+00:00

Well it might be a dumb question, but I’m unable to find an answer:

  • 0

Well it might be a dumb question, but I’m unable to find an answer:

The case is simple, I have a function with an integer in entry and two variable to assign depending of that. The problem is that the value assigned to the variables is common to certain case, but those case are not the same for the two variables. (if it’s not clear enough, see the example).

I was wondering what was the best practice for such a case.
Is it something like :

function test(a){
    var x,y;
    switch (a){
        case 0:
        case 1:
        case 7:
            y=...;
            break;
        case 2:
        case 6:
            y=...;
            break;
        case 3:
        case 4:
        case 5:
            y=...;
    }
    switch(a){
        case 5:
        case 6:
        case 7:
            x=...;
            break;
        case 0:
        case 4:
            x=...;
            break;
        case 1:
        case 2:
        case 3:
            x=...;
    }
    ...
}

or

function test(a){
    var x,y;
    switch (a){
        case 0:
            x=...;
            y=...;
            break;              
        case 1:
            x=...;
            y=...;
            break;
        case 2:
            x=...;
            y=...;
            break;
        case 3:
            x=...;
            y=...;
            break;
        case 4:
            x=...;
            y=...;
            break;
        case 5:
            x=...;
            y=...;
            break;
        case 6:
            x=...;
            y=...;
            break;
        case 7:
            x=...;
            y=...;
    }
    ...
}

Or to use a mix of the two with the value assigned to x in each case and to make groups for the value of y?

Note that there might be more than 8 value, it’s just for the example.
Thanks in advance.

  • 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-24T20:32:13+00:00Added an answer on May 24, 2026 at 8:32 pm

    If there’s no real overlap between the different cases in the switch statements, you’re probably better off having them separate.

    If there was a majority of commonality between the actions, you could combine them with certain special cases within the individual case blocks but you seem to indicate that’s not the case here.

    However, if this isn’t just a simple example of a much more complex case, you can achieve a more compact solution with something like:

    //                     index:  0  1  2  3  4  5  6  7  8  9 10 11 12
    static const int lookupX[] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9};
    static const int lookupY[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9};
    if ((a >=0) && (a < sizeof(lookupX) / sizeof(*lookupX)))
        x = lookupX[a];
    if ((a >=0) && (a < sizeof(lookupY) / sizeof(*lookupY)))
        y = lookupY[a];
    

    This will allow you to keep the values in a much smaller “configuration” section so that you can easily see the intent. The range checking and lookup then become very simple.

    That code is tailored to C – I’m not sure what specific language you’re using (because of the var statement) but it’s basically only doing the lookup if the index will be valid. You’ll need to translate that bit to your language of choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might very well be very stupid question, but I can not find QTabBar
Well the question might seem stupid but I really can't figure it out. How
Well this question might look simillar to questions easily findable with google but for
This might be a dumb question. I think I already know the answer, just
Well, that might be a strange question, and maybe just because I'm not familiar
Disclaimer I am well aware that PHP might not have been the best choice
Seems that it may not be possible, but hey I might as well ask,
That's probably not MVC specific, it might as well applicable to ASP.NET WebForms, but
I have one site that I never perused and I might as well redirect
I am a total noob so I might have gotten the basics wrong but

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.