CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Switch

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FJMSoft



Joined: 20 Oct 2013
Posts: 36

View user's profile Send private message

Switch
PostPosted: Mon Apr 28, 2014 2:01 pm     Reply with quote

Hello.

I need to make a switch which in several cases I will have a common procedure (which is also a case) to do and dont want to copy in every case (to use less memory).
How can I do this?
Can I use a goto inside a switch?

Code:
switch (MODE) {
       case 1:
         //code
         goto common;
         break;

       case 2:
         //code
         goto common;
         break;

       case 3:
common://code
         break;


Also, as above, can I put the break inside an IF?
Code:
       case 3:
common: if (x=1) break;
         //code
         break;


Sorry for such donkey questions, I'm learning. The help dont have such information.
Thank you.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

Switch
PostPosted: Mon Apr 28, 2014 2:29 pm     Reply with quote

You can put the case this way

Code:

switch (MODE) {
       case 1:
       case 3:
       case 5:
         //code
         goto common;
         break;

       case 2:
       case 4:
       case 6:
         //code
         goto common2;
         break;
}


I don't like to use goto's, I consider that as dirty code
Why no create a sub routine called Common() and call it?

Plus if you use goto's you don't need a break after since the code never come back to execute the break.

Part two

Break are used to get out of loops so if you use a break inside of an IF that is inside of a loop, like SWITCH, yes, its OK but it will ignore the rest of the code after it until the en of the SWITCH structure.
_________________
Electric Blue
FJMSoft



Joined: 20 Oct 2013
Posts: 36

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 2:43 pm     Reply with quote

Thanks for the answer E_Blue.

Actually, I have made a code like this but it is failing and I cant find the error.
I think is something about the goto.

E_Blue, can I put the label (common in this case) inside another case as shown?

My code is like:
Code:
Case 1:
        //code
       goto common

Case 5:
common: If (x==7) break;
         //code <- it is doing this code even when x=7
         break;


I think a routine would be ok.
But, can I break the switch inside it?

Thank you again.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 3:23 pm     Reply with quote

One note - it may have just been a typo on your part, but you had the statement "if (x=1) then" -- that will ALWAYS be true (and will set "x" equal to 1). The correct syntax is "if (x == 1) then" - what works even better to protect yourself (and myself) from that common mistake is to write it reversed - if you write it as " if ( 1=x) then" the compiler will kick it out as an error so you will see it and realize you meant "if (1 == x) then". I have shot myself in the foot a number of times with forgetting the second "=" sign in a conditional test (and the compiler says nothing, leaving you to stare at it for hours trying to figure out what is wrong!!)

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group