Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some interesting uses of 'switch' in C/C++? [closed]

The switch statement in C/C++ has an interesing feature that all subsequent blocks will be executed if a condition is met

For example,

int a = 2;
int b = a;
switch(b)
{
     case 1:cout<<1;
     case 2:cout<<2;
     case 3:cout<<3;
     case 4:cout<<4;
};

The above code will output 234 unless I put a break statement in case 2.

In 3 years(quite small,yeah) of my C/C++ programming experience, I have never encountered a problem where I had to use switch without putting break statments in every case. But judging by the fact that this feature has been stuck for so long, there might be some utility of it.

Question: What are some clever uses of switch statement as to utilize the above mentioned feature in C/C++?

like image 817
Cheeku Avatar asked Mar 17 '14 09:03

Cheeku


People also ask

What is the use of switch in C?

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.

What can I use instead of a switch-case in C?

You could use a series of if else statements or you could lookup a set of values in a table. You could use an array of function pointers indexed by the character.

What is switch in C with example?

Switch Case Example in C A switch construct is used to compare the value stored in variable num and execute the block of statements associated with the matched case. In this program, since the value stored in variable num is eight, a switch will execute the case whose case-label is 8.

What is important to know about switch statements?

1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed. 2) All the statements following a matching case execute until a break statement is reached. 3) The default block can be placed anywhere.


2 Answers

Probably one of the most interesting use cases I have seen would be Duff's Device the case where you extend a scope within a switch over multiple cases which would look something like this:

void send( int *to, const int *from, int  count)
{
        int n = (count + 7) / 8;
        switch(count % 8) 
        {
            case 0: do {    *to = *from++;   // <- Scope start
            case 7:         *to = *from++;
            case 6:         *to = *from++;
            case 5:         *to = *from++;
            case 4:         *to = *from++;
            case 3:         *to = *from++;
            case 2:         *to = *from++;
            case 1:         *to = *from++;
                        } while(--n > 0);    // <- Scope end
        }
}
like image 115
Shafik Yaghmour Avatar answered Oct 15 '22 01:10

Shafik Yaghmour


This is usually used when you want to apply a similar action to a set of values. For instance, the following :

switch (event) {
   case DEVICE_DISCONNECTED:
   case CONNECTION_ERROR:
   case CONNECTION_TIMEOUT:
     transitionTo(disconnectedState);
     break;
   case CONNECTION_SUCCESS:
     transitionTo(connectedState);
     break;
}

is much more concise and readable in my opinion than :

switch (event) {
   case DEVICE_DISCONNECTED:
     transitionTo(disconnectedState);
     break;
   case CONNECTION_ERROR:
      transitionTo(disconnectedState);
     break;
   case CONNECTION_TIMEOUT:
     transitionTo(disconnectedState);
     break;
   // ... 
}
like image 32
Halim Qarroum Avatar answered Oct 15 '22 03:10

Halim Qarroum