开发者

Multiple variables in switch statement in c

How to write following statement in c using switch statement in c

int i = 10;
int j = 20;

    if (i == 10 && j == 20)
    {
       Mymethod();
    }
    else if (i == 100 && j == 200)
    {
       Yourmethod();
    }
    else if (i == 1000 || j == 2000) // OR
    {
       Anymethod();
    }

EDIT:

I have changed the last case fr开发者_如何学运维om 'and' to 'or' later. So I appologise from people who answered my question before this edit.

This scenario is for example, I just wanted to know that is it possible or not. I have google this and found it is not possible but I trust gurus on stackoverflow more.

Thanks


You're pressing for answers that will unnaturally force this code into a switch - that's not the right approach in C, C++ or C# for the problem you've described. Live with the if statements, as using a switch in this instance leads to less readable code and the possibility that a slip-up will introduce a bug.

There are languages that will evaluate a switch statement syntax similar to a sequence of if statements, but C, C++, and C# aren't among them.


After Jon Skeet's comment that it can be "interesting to try to make it work", I'm going to go against my initial judgment and play along because it's certainly true that one can learn by trying alternatives to see where they work and where they don't work. Hopefully I won't end up muddling things more than I should...

The targets for a switch statement in the languages under consideration need to be constants - they aren't expressions that are evaluated at runtime. However, you can potentially get a behavior similar to what you're looking for if you can map the conditions that you want to have as switch targets to a hash function that will produce a perfect hash the matches up to the conditions. If that can be done, you can call the hash function and switch on the value it produces.

The C# compiler does something similar to this automatically for you when you want to switch on a string value. In C, I've manually done something similar when I want to switch on a string. I place the target strings in a table along with enumerations that are used to identify the strings, and I switch on the enum:

char* cmdString = "copystuff";  // a string with a command identifier, 
                                //   maybe obtained from console input

StrLookupValueStruct CmdStringTable[] = {
    { "liststuff", CMD_LIST },
    { "docalcs",   CMD_CALC },
    { "copystuff", CMD_COPY },
    { "delete",    CMD_DELETE },
    { NULL,        CMD_UNKNOWN },
};

int cmdId = strLookupValue( cmdString, CmdStringTable); // transform the string 
                                                        //    into an enum

switch (cmdId) {
    case CMD_LIST:
        doList();
        break;

    case CMD_CALC:
        doCalc();
        break;

    case CMD_COPY:
        doCopy();
        break;

    // etc...    
}

Instead of having to use a sequence of if statements:

if (strcmp( cmdString, "liststuff") == 0) {
    doList();
}
else if (strcmp( cmdString, "docalcs") == 0)  {
    doCalc();
}
else if (strcmp( cmdString, "copystuff") == 0) {
    doCopy();
}
// etc....

As an aside, for the string to function mapping here I personally find the table lookup/switch statement combination to be a bit more readable, but I imagine there are people who might prefer the more direct approach of the if sequence.

The set of expressions you have in your question don't look particularly simple to transform into a hash - your hash function would almost certainly end up being a sequence of if statements - you would have basically just moved the construct somewhere else. Jon Skeet's original answer was essentially to turn your expressions into a hash, but when the or operation got thrown into the mix of one of the tests, the hash function broke down.


In general you can't. What you are doing already is fine, although you might want to add an else clause at the end to catch unexpected inputs.

In your specific example it seems that j is often twice the value of i. If that is a general rule you could try to take advantage of that by doing something like this instead:

if (i * 2 == j) /* Watch out for overflow here if i could be large! */
{
    switch (i)
    {
        case 10:
            // ...
            break;
        case 100:
            // ...
            break;
        // ...
    }
}


(Removed original answer: I'd missed the fact that the condition was an "OR" rather than an "AND". EDIT: Ah, because apparently it wasn't to start with.)

You could still theoretically use something like my original code (combining two 32-bit integers into one 64-bit integer and switching on that), although there would be 2^33 case statements covering the last condition. I doubt that any compiler would actually make it through such code :)

But basically, no: use the if/else structure instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜