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 support@ccsinfo.com

how to do a switch with variables

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



Joined: 07 Jan 2006
Posts: 64
Location: Braga

View user's profile Send private message Send e-mail MSN Messenger

how to do a switch with variables
PostPosted: Sun Oct 29, 2006 10:05 am     Reply with quote

i have this code :

SWITCH (com) {

Case 62 : output_high(L1); // TECLA 3 TV
delay_ms(250);
output_low(L1);
com=0;
break;

Case 61 : output_high(L2); // TECLA 2 TV
delay_ms(250);
output_low(L2);
com=0;
break;

Case 60 : output_high(L3); // TECLA 1 TV
delay_ms(250);
output_low(L3);
com=0;
break;
// Everything is fine ------------------------------------------------------
but i want something like this

SWITCH (variable) {

Case (variable1) : output_high(L1); // TECLA 3 TV
delay_ms(250);
output_low(L1);
com=0;
break;

Case (variable2) : output_high(L2); // TECLA 2 TV
delay_ms(250);
output_low(L2);
com=0;
break;

Case (variable3) : output_high(L3); // TECLA 1 TV
delay_ms(250);
output_low(L3);
com=0;
break;

Any Ideas , please ?
Ttelmah
Guest







PostPosted: Sun Oct 29, 2006 2:19 pm     Reply with quote

"The _switch_ statement, is a multi-way decision, that tests whether an expression matches one of a number of _constant_ integer values, and branches accordingly". This from Kerninghan & Ritchie, Note the _constant_...

Best Wishes[/i]
Guest








PostPosted: Sun Oct 29, 2006 5:04 pm     Reply with quote

Hi all,

As Ttelmah said, the switch is limited to constants.

BUT,

Here are some thoughts...

#1. Instead of variable within the switch, can you use "variables" that are defined within a "#define" statement?

Code:

#define TECLA_3_TV  62
.
.
.
     switch(com)
     {
          case(TECLA_3_TV): bla bla bla
      .
      .
      .
     }


#2. Don't use a switch!!! Can you use something like
Code:

.
.
.
    if(com==variable1) {// do variable 1 stuff}
    elseif(com==variable2} { }
    elseif(com==variable3} { }
.
.
.


When you look at the logic of what you want, you are just comparing com with your variable.

#3. Good ol' pointers to functions!
Code:

.
.
.
void (*pof)[10];    // array if 10 pof's
.
pof[0]=&func1;    // assign them manualy like this, or [better] Assign them in the def above
.
.
pof[com]();    // call it


yes yes yes, this take memory if your index's are not seqencial
yes yes yes CCS does not support this...at least not v3x, I forget if v4x does... Sad

Well, thats food for thought

~Kam (^8*
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

pointer-to-function... Mmmm...
PostPosted: Sun Oct 29, 2006 6:05 pm     Reply with quote

Anonymous wrote:
yes yes yes CCS does not support this...at least not v3x, I forget if v4x does... Sad

v4.x should support pointer-to-function. It seems that this feature is being debugged by CCS. There's a lot of cool tricks that can be done with pointer-to-function...
Guest








Re: pointer-to-function... Mmmm...
PostPosted: Sun Oct 29, 2006 8:54 pm     Reply with quote

kender wrote:
There's a lot of cool tricks that can be done with pointer-to-function...


So true...so true...that opens up many programming techniques...pre oo progrmaming! CCS you're nearly a C compiler! Very Happy

~Kam (^8*
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