View unanswered posts | View active topics It is currently Wed Oct 01, 2014 1:56 pm






Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Can Switch..Case statements b used 2 capture ranges of value 
Author Message
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post 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.

Code:
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
   PlaySound(soundDownwardTones);
   eraseDisplay();
   nxtDisplayString(0, "Case 33");
  break;

  case  66:
  PlaySound(soundBeepBeep);
   eraseDisplay();
   nxtDisplayString(0, "Case 66");
  break;

  case  100:
  PlaySound(soundFastUpwardTones);
   eraseDisplay();
   nxtDisplayString(0, "Case 100");
  break;

  default:
  PlaySound(soundException);
  eraseDisplay();
   nxtDisplayString(0, "Case Exception");
}

  wait10Msec(300);
}



Thanks


Sat Mar 17, 2007 2:05 am
Profile
Rookie

Joined: Fri Mar 16, 2007 9:12 am
Posts: 26
Post 
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
Profile
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post 
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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
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
Profile
Rookie

Joined: Fri Mar 09, 2007 12:02 am
Posts: 11
Post 
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
Profile YIM
Rookie

Joined: Fri Mar 16, 2007 9:12 am
Posts: 26
Post 
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..

:P


Fri Mar 23, 2007 10:40 pm
Profile
Rookie

Joined: Fri Mar 09, 2007 12:02 am
Posts: 11
Post 
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
Profile YIM
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post 
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


Sun Mar 25, 2007 12:20 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
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
Profile WWW
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post 
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:


Mon Mar 26, 2007 8:53 am
Profile
Rookie

Joined: Fri Mar 09, 2007 12:02 am
Posts: 11
Post 
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
Profile YIM
Rookie

Joined: Sat Jan 19, 2008 1:35 pm
Posts: 5
Post 
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?


Code:
 switch(nMotorEncoder[motorB])


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


Sat Jan 19, 2008 1:47 pm
Profile
Rookie

Joined: Mon Oct 29, 2007 4:25 pm
Posts: 7
Location: Potsdam, Germany
Post 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
Profile
Expert
User avatar

Joined: Fri Nov 09, 2007 4:51 am
Posts: 121
Location: Hungary, Europe
Post 
sargunster100 wrote:
Could you please tell me what is wrong?
Code:
 switch(nMotorEncoder[motorB])


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


Tue Jan 22, 2008 5:24 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
Quote:
sargunster100 wrote:
Could you please tell me what is wrong?
Code:
switch(nMotorEncoder[motorB])

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
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 17 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: nxt and 2 guests


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 post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.