Can Switch..Case statements b used 2 capture ranges of value
Page 1 of 2

Author:  JamesD [ Sat Mar 17, 2007 2:05 am ]
Post subject:  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.

int nSpeed = 30;
//This value is hardcoded in to test the different outcomes. At this stage any outcome other
//than 33, 66 & 100 is handled by the default option.

task main()

switch (nSpeed)
   case  33:   //case  < 33 doesn't work
   nxtDisplayString(0, "Case 33");

  case  66:
   nxtDisplayString(0, "Case 66");

  case  100:
   nxtDisplayString(0, "Case 100");

   nxtDisplayString(0, "Case Exception");



Author:  HoTWiReZ [ Sat Mar 17, 2007 3:32 pm ]
Post subject: 

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..

Author:  JamesD [ Sun Mar 18, 2007 4:43 am ]
Post subject: 

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. :)

Author:  Dick Swan [ Fri Mar 23, 2007 3:25 pm ]
Post subject: 

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

case 5:
case 6:
case 7:
// Do code for case 5 to 7 here

case 8:
case 9:
case 10:
// Do code for case 8 to 10 here

Author:  sessyargc [ Fri Mar 23, 2007 7:23 pm ]
Post subject: 

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.

Author:  HoTWiReZ [ Fri Mar 23, 2007 10:40 pm ]
Post subject: 

HoTWiReZ wrote:
I'm pretty sure that it wouldn't work.. I usually stick with if statements for that sort of stuff too.

Just thought I'd point that out.. I'm not being defensive or anything..


Author:  sessyargc [ Fri Mar 23, 2007 11:41 pm ]
Post subject: 

Sorry, my comment wasn't meant to be offensive either. Sorry, if I've offended anyone with my comments.


Author:  JamesD [ Sun Mar 25, 2007 12:20 am ]
Post subject: 

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 :D

Author:  starwarslegokid [ Sun Mar 25, 2007 7:08 pm ]
Post subject: 

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?


Author:  JamesD [ Mon Mar 26, 2007 8:53 am ]
Post subject: 

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. :P :roll: :wink:

Author:  sessyargc [ Wed Mar 28, 2007 12:33 am ]
Post subject: 

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 ( ... 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.


Author:  sargunster100 [ Sat Jan 19, 2008 1:47 pm ]
Post subject: 

I tried this code but it comes up with an error:

**Error**:Array specifier invalid for variable 'nMotorEncoder'

Could you please tell me what is wrong?


:roll: :cry: :?: :?: :?: :?: :?: :?: :?: :?:

Author:  MK [ Sun Jan 20, 2008 12:59 pm ]
Post subject:  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]

Author:  elemes [ Tue Jan 22, 2008 5:24 am ]
Post subject: 

sargunster100 wrote:
Could you please tell me what is wrong?

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:
mb = nMotorEncoder[motorB];
if( mb < your absolute lower limit ) {
  your action below reaching the lower limit;
else if( mb < closer lower limit ) {
  your action when you are close to your target range;
else if( mb > your absolute upper limit ) {
  your action far over the upper limit;
else if( mb > closer upper limit ) {
  your action when you are slightly over the upper limit;
else {
  your action when motor is within the accepted range;
  keep in mind the motor's momentum, it will turn a bit;
  even if you immediately stop it;
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.

Author:  Dick Swan [ Tue Mar 04, 2008 8:15 pm ]
Post subject: 

sargunster100 wrote:
Could you please tell me what is wrong?

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.

Page 1 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group