Author |
Message |
JamesD
Novice
Joined: Sun Feb 04, 2007 12:48 am Posts: 69 Location: Australia
|
 Can Switch..Case statements b used 2 capture ranges of value
Hi All,
The following code uses a Case statement to handle different outcomes based on the value of variable nSpeed. Is it possible to use Case statements with the < and > operators such that I can capture and handle ranges of values such as 0-33 & 34-66 & 67-100 in only three case segments? I know it can be done using If statements but I’m curious about Case statements.
ie I'd like to capture any value b/n 0-33 in the first segment. 34-66 in the second segment etc.
Thanks
|
Sat Mar 17, 2007 2:05 am |
|
 |
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
Hey, I don't think I've ever seen switch statements used like you're wanting.. But, I imagine if it could, I'd try something like
case (nSpeed<33)
but I'm pretty sure that it wouldn't work.. I usually stick with if statements for that sort of stuff too. Just a little more typing, but I don't think it makes the program any more complicated.. Also, you could say case 33, case 34, ... but I think that's a bit much too, hehe..
|
Sat Mar 17, 2007 3:32 pm |
|
 |
JamesD
Novice
Joined: Sun Feb 04, 2007 12:48 am Posts: 69 Location: Australia
|
Thanks HoTWiReZ,
I tried to use:
case (nSpeed<33)
and as you suspected no luck. Oh well, thanks for your advise and help. It looks like I was trying to use it incorrectly. 
|
Sun Mar 18, 2007 4:43 am |
|
 |
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 616
|
RobotC implementation is standard C format.
A little awkward, but you could write as
case 0:
case 1:
case 2:
case 3:
case 4:
// Do code for cases 0 to 4 here
break;
case 5:
case 6:
case 7:
// Do code for case 5 to 7 here
break;
case 8:
case 9:
case 10:
// Do code for case 8 to 10 here
break;
|
Fri Mar 23, 2007 3:25 pm |
|
 |
sessyargc
Rookie
Joined: Fri Mar 09, 2007 12:02 am Posts: 11
|
re: case (nSpeed<33)
Is an invalid C code. As RobotC tries to implement the C syntax the above code is indeed invalid.
Dick Swan's approach is OK. Remember by case-statements fall-through, so it will work.
Another approach will be to create a function that checks the range and returns a value if the number is in a specific range. Then use the return value in your switch-statement. It's also cumbersome, but it should work.
Lastly, why not use the if-statement and implement your idea using an if-else-if ladder? Its more appropriate for the job.
|
Fri Mar 23, 2007 7:23 pm |
|
 |
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
Just thought I'd point that out.. I'm not being defensive or anything..

|
Fri Mar 23, 2007 10:40 pm |
|
 |
sessyargc
Rookie
Joined: Fri Mar 09, 2007 12:02 am Posts: 11
|
Sorry, my comment wasn't meant to be offensive either. Sorry, if I've offended anyone with my comments.
Regards,
Rommel.
Last edited by sessyargc on Sun Mar 25, 2007 1:22 am, edited 1 time in total.
|
Fri Mar 23, 2007 11:41 pm |
|
 |
JamesD
Novice
Joined: Sun Feb 04, 2007 12:48 am Posts: 69 Location: Australia
|
Thanks guys,
Absolutely no offense taken!!
I'm just glad to be able to bounce questions and ideas around in the forum. I really appreciate that you guys took the time to respond and help clarify my understanding of the use of CASE statements.
The more I understand this stuff the more I can pass on to the kids I teach. So thank you 
|
Sun Mar 25, 2007 12:20 am |
|
 |
starwarslegokid
Moderator
Joined: Wed Jan 31, 2007 3:39 am Posts: 299 Location: San Diego, California. USA
|
I'm unfamiliar with the differences in Case statements and If statements, they appear very similar. What are the similarities and differences between the two?
Thanks!
_________________Mmmm Legos B-) My Robot Projects: http://www.freewebs.com/robotprojects/
|
Sun Mar 25, 2007 7:08 pm |
|
 |
JamesD
Novice
Joined: Sun Feb 04, 2007 12:48 am Posts: 69 Location: Australia
|
Now, I'm no expert (as you can see from my posts above) but here goes and I'm happy to be corrected:
My understanding is that Case statements are used when you are evaluating a variable which may have a number of fixed and identifiable outcomes. If you have two or three likely outcomes to resolve use if statements however if you need to respond to say 8 different outcomes separately then use CASE statements.
1) They are more efficient/quicker for the CPU to process/execute.
2) They are easier for programmers (including yourself) to read/decipher than a series of If statements one after the other.
And yes they do appear to be similar in the sense that what ever you put in a CASE statement can be done with IF statements but without the finesse that I know we so desperately seek. 
|
Mon Mar 26, 2007 8:53 am |
|
 |
sessyargc
Rookie
Joined: Fri Mar 09, 2007 12:02 am Posts: 11
|
FWIW, gcc (at least 3.2.3) supports something like this:
case 1...5:
Haven't actually tried it but I got it from the gcc manual ( http://gcc.gnu.org/onlinedocs/gcc-3.2.3 ... e%20Ranges). Still it's only integers and no floats allowed. I assume its non-portable since I don't remember reading this in the C specs. I don't know if RobotC supports this.
Am no expert either, though I've been using C for a decade now. I agree with JamesD's comments.
To me, the selection between which construct to use is mostly cosmetic. If I'm checking integers only with specific values (or cases) I choose the switch-case. If I'll be using bitwise operators, logical operator or comparisons I automatically use an if-else. Most of the time, both constructs generate almost the same assembly code.
HTH.
|
Wed Mar 28, 2007 12:33 am |
|
 |
sargunster100
Rookie
Joined: Sat Jan 19, 2008 1:35 pm Posts: 5
|
|
Sat Jan 19, 2008 1:47 pm |
|
 |
MK
Rookie
Joined: Mon Oct 29, 2007 4:25 pm Posts: 7 Location: Potsdam, Germany
|
 Case statement
You could map the value of nSpeed to a range of integers like
nCase = nSpeed / 33 which should give
0 for nSpeed in 0..32
1 for nSpeed in 0..66
2 for nSpeed in 0..99
the cases are then 0, 1, 2, ... and cover the required ranges.
You could use a float number instead of 33, then the integer part needs to be extracted explicitly if the case statement does not coerce the argument to integer.
However, the approach is somewhat obscure and does not work for arbitrary ranges unless you find a suitable mapping function. I would prefer nested ifs by readibility considerations.
Best regards, Martin Kraska[/code]
|
Sun Jan 20, 2008 12:59 pm |
|
 |
elemes
Expert
Joined: Fri Nov 09, 2007 4:51 am Posts: 121 Location: Hungary, Europe
|
1. Some cases RobotC is a little bit more restrictive, compared to standard C. For such cases I suggest using a more simple structure e.g. introducing a new temporary variable. 2. switch statement allows you branching between discrete values. resoution of motor encoder is too fine for such purpose -- the encoder value steps 360 during one rotation of the motor axle and can be anywhere in the range of -32768 .. +32767. You might want to try an "if-else" ladder: In such a structure you can slow down the motor when you are close to the target and thus avoid "overshoot." If it is the intended operation of course.
|
Tue Jan 22, 2008 5:24 am |
|
 |
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 616
|
You've done nothing wrong. I tried this with version 1.17 and could not find any compiler error. I might have fixed something previously that caused an error but I cannot recall.
|
Tue Mar 04, 2008 8:15 pm |
|
|