How to handle multiple cases which use common code?

This is a discussion on How to handle multiple cases which use common code? within the C Programming forums, part of the General Programming Boards category; I have a situation where multiple cases within a switch case operation use common code. The only difference between the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    Question How to handle multiple cases which use common code?

    I have a situation where multiple cases within a switch case operation use common code. The only difference between the cases is that I need to set up some counter variables to be unique for each case. Instead of duplicating the common code is there a way to set up the variables based on the case and then direct flow to the common code? I can't just put the common code at the end of the switch statement since the case statements below are embedded in alot of other case statements. See the cumbersome way (duplicating the common code for each case) of doing this below - the common code is in red:

    Code:
    switch(input)
    {
    	case 1:
    	    i=0;
                  	    k=5;
                   
    	   while (i< k)
                      {
                         if (mode == DATASTREAM)
                         {
                            for(j=0; j<5; j++)
                            {
                               if(C_Support & (1 << j))
                               {
                                  msg_data[msg_idx++] = GET_BYTE_A(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_B(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_C(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_D(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_A(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_B(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_C(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_D(p_emm_timer2[i]);
                               }
                               else msg_idx += 8;
                            }
                         }
    	      i++;
                      }	break;
    
    	case 0x82:
                   i=4;
                   k=10;
    
    	   while (i< k)
                      {
                         if (mode == DATASTREAM)
                         {
                            for(j=0; j<5; j++)
                            {
                               if(C_Support & (1 << j))
                               {
                                  msg_data[msg_idx++] = GET_BYTE_A(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_B(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_C(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_D(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_A(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_B(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_C(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_D(p_emm_timer2[i]);
                               }
                               else msg_idx += 8;
                            }
                         }
    	      i++;
                      }	break;
                   
    	case 0x89:
                    i=8;
                    k=15;
    
    	   while (i< k)
                      {
                         if (mode == DATASTREAM)
                         {
                            for(j=0; j<5; j++)
                            {
                               if(C_Support & (1 << j))
                               {
                                  msg_data[msg_idx++] = GET_BYTE_A(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_B(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_C(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_D(p_emm_timer1[i]);
                                  msg_data[msg_idx++] = GET_BYTE_A(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_B(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_C(p_emm_timer2[i]);
                                  msg_data[msg_idx++] = GET_BYTE_D(p_emm_timer2[i]);
                               }
                               else msg_idx += 8;
                            }
                         }
    	      i++;
                      }	break;

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    create a function.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Create a function that has the common code and takes as parameters i and k and call it in each case. Put inline or even use a macro if you are REALLY worried about performance.

    EDIT: Actually, why not have the switch statement to set i and k and afterward ONCE the common code?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Just pull the common code out of the switch. All you need the switch for is to set the i and k values.

    Edit : I didn't see that C_ntua already suggested this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. multiple cases
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-09-2002, 11:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21