Thread: Effecient Selection Statements?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Effecient Selection Statements?

    Hi there, I was wondering if there is a more efficient way of doing the following without having to re-include parts of the code?

    Code:
    	if (num_channels==1){*out1++=0;}
    	else if (num_channels==2){*out1++=0; *out2++=0;}
    	else if (num_channels==3){*out1++=0; *out2++=0; *out3++=0;}
    	else {*out1++=0; *out2++=0; *out3++=0; *out4++=0;}
    This is a simple example but in my code there is a lot of work to be done for each channel which would make the program much smaller if I didn't have to re-include the code each time. Also my program might be extended to 32 channels which would leave lots of extra code lying around in these if statements.

    I had a look at using switch statements but this didn't compile as I have some declarations which could potentially be skipped.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Switch/case seems fine to me. Note the use of fall-through.
    Code:
        switch ( outchannels ) {
            case 4:
                *out4++=0;
            case 3:
                *out3++=0;
            case 2:
                *out2++=0;
            case 1:
                *out1++=0;
                break;
            default:
                break;
        }
    > Also my program might be extended to 32 channels
    Isn't that in the realm of rewriting the code to deal with an array of channels, which would make everything a whole lot simpler than having lots of code to repeat each channel number.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem on - Selection on which item to purchase
    By karipap in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 06:19 AM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. selection problem
    By Ken JS in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:47 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Replies: 19
    Last Post: 01-31-2006, 04:21 PM