Switch case help!!

Discussion in 'Programmer's Corner' started by paddy611, Dec 4, 2012.

  1. paddy611

    Thread Starter New Member

    Oct 9, 2012
    25
    0
    Hello,

    I am using switch case function in code.

    Can i use multiple statements in each case or it should be single statement in each case?

    thanks,
    paddy
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    you should have at least two statements per case: one/many to perform your desired operation, and one to break out of the switch statement, otherwise, you will fall into the next case!
     
  3. paddy611

    Thread Starter New Member

    Oct 9, 2012
    25
    0
    That means the code i have written below is valid?

    Code ( (Unknown Language)):
    1.  
    2.  
    3. switch(i)
    4.             {
    5.                case 0:
    6.                output_high(pin_B7);        //turn on LED              
    7.                output_high(pin_A2);        //clock to latch
    8.                delay_us(100);
    9.                output_low(pin_A2);
    10.                delay_us(100);
    11.                output_low(pin_B7);        //turn off LED              
    12.                output_high(pin_A2);       //clock to latch
    13.                delay_us(100);
    14.                output_low(pin_A2);
    15.                delay_us(100);
    16.                break;
    17.  
    18. }
    19.            
    thanks
     
    Last edited: Dec 4, 2012
  4. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    1) You've never indicated what language you are using. C?

    2) The above code is not correct because you have a dangling block. You open the block with '{' but never close it.
     
  5. paddy611

    Thread Starter New Member

    Oct 9, 2012
    25
    0
    Ohhh I am sorry!!!

    I am using C language.

    I just want to confirm that the switch case supports multiple statements or not.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    Yes, it does. As point out by someone else, it is almost worthless if you don't have multiple statements because you almost always need a break statement at the end of each case option.
     
  7. paddy611

    Thread Starter New Member

    Oct 9, 2012
    25
    0
    Ok thanks a lot for help!!!
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
Loading...