tags:

views:

83

answers:

4

hello, I was just wondering if someone could just give me an example of how to use switch/case. I kinda get the just of it but am having trouble figuring out what and how i can use this. Thank you in advance.

A: 

Say you have an enum

enum expr_type {
    EXPR_TYPE_ADD,
    EXPR_TYPE_SUBTRACT,
    EXPR_TYPE_GET_VALUE
};

We can do a switch on this:

enum expr_type t = /* get input somehow and find the type */;

switch(t) {
    case EXPR_TYPE_ADD:
         cout << "Operator Add";
         /* fall through */
    case EXPR_TYPE_SUBTRACT:
         cout << "Operator (Add or Subtract)";
         break;
    case EXPR_TYPE_GET_VALUE;
         cout << "Getting some value";
         break;
}

You have to put in the break; so it doesn't fallthrough - Currently, EXPR_TYPE_ADD will exute all the code for EXPR_TYPE_SUBTRACT. Make sure to use break correctly!

mathepic
+1  A: 

Here is a fairly typical use case. You have a list of values (the enum) and a switch which checks the input to determine which you are dealing with. This assumes of course that the action you will take depends on the underlying value of the enum.

enum ImageFormat
{
    FormatRGB888,
    FormatRGB8888,
    FormatRGB101010,
    FormatRGB161616,
    FormatRGB16161616
};

void SomeFunc(ImageFormat format)
{
    switch(format)
    {
        case FormatRGB888:
            // do stuff
            break;
        case FormatRGB8888:
            // do stuff
            break;
        case FormatRGB101010,
            // do stuff
            break;
        case FormatRGB161616:
            // do stuff
            break;
        case FormatRGB16161616:
            // do stuff
            break;
        default:
            // bad value for 'format'
    }
}
Ed Swangren
thank you. so basicallt it can add more if's and elses then.
Justin Yoder
Well, you can always use if/else if, but switches give you 1. constant lookup time and 2. fallthrough if needed (though #2 can be problematic when you fallthrough by accident)
Ed Swangren
+2  A: 

There are couple of things to remember about switch case statements:

a) The condition should be integeral/enum/user defined type which supports conversion to int or enum

b) case lables are compile time constants

c) No two case label expressions can have the same value

d) $6.4.2/5- "When the switch statement is executed, its condition is evaluated and compared with each case constant. If one of the case constants is equal to the value of the condition, control is passed to the statement following the matched case label. If no case constant matches the condition, and if there is a default label,control passes to the statement labeled by the default label. If no case matches and if there is no default then none of the statements in the switch is executed."

e) $6.4.2/6- "case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels. To exit from a switch, see break"

enum direction {north, south, east, west};
char x;
class UD{
    operator int(){return 0;}
};

direction f1(){
    return north;
}

char f2(){
    return 'A';
}

int main(){
    direction d = f();
    string country;

    // switch condition of type enum
    switch(d){
    case north:
        country = "P";
        break;
    case south:
        country = "Q";
        break;
    case east:
        country = "R";
        break;
    case west:
        country = "S";
        break;
    default:
        country = "";
        break;
    }

    // switch condition of integral type
    switch(c){
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
        cout << "Vowel";
        break;
    default:
        cout << "Not a Vowel";
        break;
    }

    UD u;

    // switch condition of user defined type (conversion to integral type)
    switch(u){
    case 0:
    case 1:
        cout << "Good";
        break;
    default:
        cout << "Not so good";
        break;
    }
}
Chubsdad
A: 

Switch statements are a more efficient way of doing a lot of ifs and elses.

Sheldon