Switch case help!!

Thread Starter

paddy611

Joined Oct 9, 2012
25
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
 

tshuck

Joined Oct 18, 2012
3,534
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!
 

Thread Starter

paddy611

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

Rich (BB code):
switch(i)
            {
               case 0:
               output_high(pin_B7);        //turn on LED               
               output_high(pin_A2);        //clock to latch
               delay_us(100);
               output_low(pin_A2);
               delay_us(100);
               output_low(pin_B7);        //turn off LED               
               output_high(pin_A2);       //clock to latch
               delay_us(100);
               output_low(pin_A2);
               delay_us(100);
               break;

}
thanks
 
Last edited:

WBahn

Joined Mar 31, 2012
33,109
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.
 

Thread Starter

paddy611

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

I am using C language.

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

WBahn

Joined Mar 31, 2012
33,109
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.
 
Top