C Switch Statements

When I first began programming I remember I designed a tic-tac-toe game in C that had a massive if-else statement, probably with 15 or so if, else if’s…it was awful and it looked awful too. Since then I have moved on to using the switch statement. Using a switch statement in any imperative programming language usually has many advantages including: easier to read, easier to debug, easier to understand, and easier to maintain. There are also code efficiency benefits because the compiler will implement the switch statement as an indexed branch table (or jump table). This improves the programs flow immensely.

I would like to compare an if statement then show you its implementation via a switch statement.

The Gross If Else

int x;
 
if(x == 1){
 // ... do something
}else if(x == 2){
 // ... do something
}else if(x == 5){
 // ... do something
}else if(x == 12){
 // ... do something crazy
}

This could go on for ages, and as you can see everything sort of get mumbled together. It is not as clear as it could be. Okay, lets compare that same example but I will take advantage of the C switch statement.

Yay Switch Statement!

int x;
 
switch(x){
     case 1:
        // ... do something
        break;
     case 2:
        // ... do something
         break;
     case 5:
        // ... do something
        break;
     case 12:
        // ... do something crazy
        break;
     default:
        // ... we didnt match anything
         break;
     }

The default case is a ‘catch-all’, meaning if ‘x’ doesn’t match any of the above options then default will be hit, in most cases it is not relevant because the programmer will know which values will be seen or not seen.

The break statements tell the program to go to the end of the switch statement. The switch statement is elegant in that you can use this to better organize functionality. For example, case one could indicate process this type of data, i.e. traverse this path, and case two could indicate to remove this type of data, i.e. traverse the delete data path. A limitation of the switch statement is its inability to deal with strings. The switch can only act upon numeric values. However, you can do some useful things by creating an enumeration so your switch statement can then have more descriptive names to outline a code path. For instance:

enum state
{
   STATE_UP = 0,
   STATE_DOWN,   // This will automatically be 1
   STATE_LEFT,   // 2
   STATE_RIGHT   // 3
};
 
switch( state )
{
   case STATE_UP:
       call_up_function();     
       break;
 
   case STATE_DOWN:  
       call_down_function();   
       break;
 
   case STATE_LEFT: 
       call_left_function();    
       break;
 
   case STATE_RIGHT:
   case 12:
       call_right_function();
       break;
}

Notice in the above example I combine STATE_RIGHT and 12, that means if we match on either STATE_RIGHT or 12 then the call_right_function() will be executed. This is known as a fall-through block, it would be equivalent to:

if(x == STATE_RIGHT || x == 12){
   call_right_function();

There you have it. In using a switch statement you can clean up your code, make it more efficient and easier to read!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *