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

help with switch statement

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



Joined: 12 May 2004
Posts: 38

View user's profile Send private message

help with switch statement
PostPosted: Mon Jul 18, 2005 9:08 am     Reply with quote

Hi
I need to define some constants and the value of these constants depends on the voltage on my A/D pin, so I thought use a switch statement but I can't seem to get it right, I have the following

Code:

switch (select)
{
case <68:
#define vmin  52
#define vconnect 104
}

...14 more cases hear



If I remove the '<' symbol it compiles fine.

None of the examples in the manual show using the select statement if the case value is in a range but I'm sure I have seen this being used before.

thanks

JFK
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

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

PostPosted: Mon Jul 18, 2005 9:32 am     Reply with quote

Wrong language and code does not make sense. In C this has to be a constant or multiple statements such as :
Code:

switch (select)
   {
   case 65:
   case 68:
      // do something here
       break;
   }


Also the #define is used by the pre-processor during compilation. But you are trying the do this based on run time code execution. Instead you need to be using variables
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
JohnKennedy



Joined: 12 May 2004
Posts: 38

View user's profile Send private message

PostPosted: Mon Jul 18, 2005 9:41 am     Reply with quote

Ok

That might have been in another lifetime when I was doing something in Visual Basic, guess the only way to achieve what I want is multiple IF statements.

Thanks for pointing out the error on the defines, though curiously it did compile without even a warning, though I hadn't tested it to see if it worked. The defines were defined before being used by the main body of the program so maybe thats why it compiled ok.

Thanks again

JFK
Ttelmah
Guest







PostPosted: Tue Jul 19, 2005 2:23 am     Reply with quote

There will be no syntax error on the defines. It is perfectly acceptable to put them anywhere in the code, _but_ they will all be executed before the program runs. Hence the compiler won't complain.
In C++ (but not in C), the switch statement can be used with 'ranges', and it may be this version you have seen.

Best Wishes
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