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

'Switch' statement question

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








'Switch' statement question
PostPosted: Thu Apr 03, 2008 6:04 am     Reply with quote

Hi All,

Does 'C' have the ability to specify more than one condition for a single 'Case' statement? In other words, I want the code to do the same thing for 2 seperate inputs, but I'd like to economize the code.

Code:

Case 'A':
    break;

Case 'B"
    break;


Would become something like:

Code:

Case 'A'; 'B':
    break;


Unfortunately, I can't find a syntax that will compile.

Thanks,

Dave
Matro
Guest







PostPosted: Thu Apr 03, 2008 6:14 am     Reply with quote

That's possible by doing the following way:
Code:

case 'A':
case 'C':
  break;


Some compilers support this way:
Code:

case 'A','C':  //execution for both 'A' and 'C'
  break;


And also the "ranges" :
Code:

case 'A'-'C':  //execution for 'A', 'B' and 'C'
  break;


Matro.[/code]
Matro
Guest







PostPosted: Thu Apr 03, 2008 6:22 am     Reply with quote

After some tests, CCS only supports the standard C switch-case statement (the first I previously wrote).
I suddendly have some doubts about the character that is used for compiler supporting ranges. It should be another one that '-' but I can't remember.
Anyway this is not supported by CCS.

Matro.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Apr 03, 2008 7:23 am     Reply with quote

case 'A' - 'C': in C would basically be the same as
case ('A' - 'C'): or case -2: It will subtract the value of 'C' from 'A'!

I don't think I have ever seen a ANSI C compiler accept ranges for a select/case statement.

I have seen it in Visual Basic!


You just have to remember that in C a select/case statement will fall through to the next statement until either a break or the end of the select is encountered!

So
Code:

select (val) {
case 1:
  a();
case 2:
case 3:
  b();
  break;
case 4:
  c();
default:
  d();
}


Would result in
a() and b() being called with val = 1
b() being called with val = 2 or 3
c() and default with val = 4
d() with val = any other number.
Matro
Guest







PostPosted: Thu Apr 03, 2008 8:07 am     Reply with quote

Wayne_ wrote:
case 'A' - 'C': in C would basically be the same as
case ('A' - 'C'): or case -2: It will subtract the value of 'C' from 'A'!

That's why I'm sure that is not '-' but I can't remember.
Quote:

I don't think I have ever seen a ANSI C compiler accept ranges for a select/case statement.

I have seen it in Visual Basic!

I already saw that in a C compiler (I can't remember ANSI or not). But it wasn't a C compiler for embedded C.
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Thu Apr 03, 2008 8:29 am     Reply with quote

As Matro said - if you omit the "break" in a case statment, execution flows into the following case - often handy.

case 'A':
// no break here (I always add this comment)
case 'B':
break;

This is also a common "bug" - forgetting the "break" - that's why I add the comment, to remind meself that this was intentional.


Ken
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Apr 03, 2008 8:33 am     Reply with quote

In the switch() statement, once a CASE: has been entered it will continue the program flow until a break; has been reached. For instance:

Code:
switch(variable)
{
  CASE 1:
    function_1();
    break;  // normal exit
  CASE 2:
    function_2();
  CASE 3:
    function_3();
    break;
  default:
    break;
}


Now, since I 'forgot' to place a break; at the end of CASE 2, function_2() will be called and then function_3() will be called. You can have multiple CASE conditions do the same thing by placing them together. For example:
Code:
switch(variable)
{
  CASE 1:
    function_1();
    break;
  CASE 2:
  CASE 3:
  CASE 7:
    function_2();
    variable2++;
    break;
  CASE 4:
    function_3();
    break;
  default:
    break;
}


I've never known of entering a 'range' for the CASE. If you do need a range then you will need to enter a CASE for each possible result.

Ronald
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