View previous topic :: View next topic |
Author |
Message |
Guest
|
'Switch' statement question |
Posted: Thu Apr 03, 2008 6:04 am |
|
|
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
|
|
Posted: Thu Apr 03, 2008 6:14 am |
|
|
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
|
|
Posted: Thu Apr 03, 2008 6:22 am |
|
|
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
|
|
Posted: Thu Apr 03, 2008 7:23 am |
|
|
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
|
|
Posted: Thu Apr 03, 2008 8:07 am |
|
|
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
|
|
Posted: Thu Apr 03, 2008 8:29 am |
|
|
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
|
|
Posted: Thu Apr 03, 2008 8:33 am |
|
|
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 |
|
|
|