Switch case?

What is wrong with this code and how else should it be done? I can do this in VB:

  switch (distance) {
    case <= 10:
      Serial.println("far away");
      break;
    case 11-50:
      Serial.println("within sight");
      break;
    case 51-100:
      Serial.println("slow down");
      break;
    case 101-200:
      Serial.println("almost there");
      break;
    case > 201:
      Serial.println("stop!");
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
  }

Cases can not use a range.

Edit: Oops. Learned something new today.

Switch statements can only compare to one value. If you want to use a range you will need if..else statements.

C and VB are different ...

...if..else....not very elegant....must be something better....

warren631:
...if..else....not very elegant....must be something better....

You have a bunch of conditionals to check. You really don't need the "else" aspect. If the value is 10, it is only going to be true in one range.

I guess that is not so bad:

if (distance<= 10) strg = "far away"; 
if (distance >10) strg = "within sight";
if (distance >50) strg = "slow down";
if (distance >100) strg = "almost there";
if (distance >200) strg = "stop!";
Serial.println(strg);

with that set the distance value being 201 will cause the string to be "within sight" then "slow down" then "almost there" then "stop!" and finally "stop!" will print.

This may not matter at all depending on what else is going on in the code though.....

warren631:
What is wrong with this code and how else should it be done? I can do this in VB:

You can do ranges. This compiles:

void setup ()
{
int distance;
  
   switch (distance) {
    case 0 ... 10:
      Serial.println("far away");
      break;
    case 11 ... 50:
      Serial.println("within sight");
      break;
    case 51 ... 100:
      Serial.println("slow down");
      break;
    case 101 ... 200:
      Serial.println("almost there");
      break;
    default: 
      Serial.println("stop!");
      break;
  }

}
void loop () {}

I learned something today!

BTW, the reason for using if..else rather than just straight if is that execution will stop as soon as the correct range is found. Using just if will mean every single condition will be tested even if the first if was executed as the correct case.

Nick,

does that trick work for if statements as well? is it possible for someone to:

if ( myVal 50 ... 100 ) Serial.println("Between 50 and 100");

This strikes me as the most elegant solution, mainly because it avoids needing to duplicate the boundary values and precludes any possibility of gaps or overlaps between the ranges. Using the switch/case range approach involves some repetition (which is always a bad idea) and would make it very easy to introduce bugs if you wanted to adjust any of the thresholds.

if(distance < 11)
{
    Serial.println("far away");
}
else if(distance < 51)
{
    Serial.println("within sight");
}
else if(distance < 101)
{
    Serial.println("slow down");
}
else  if(distance < 201)
{
    Serial.println("almost there");
}
else
{
    Serial.println("stop!");
}

Goofballtech:
Nick,

does that trick work for if statements as well? is it possible for someone to:

if ( myVal 50 ... 100 ) Serial.println("Between 50 and 100");

No.

darn the luck. that would have been sweet.

Can a logical AND be used to put the values in brackets for use with an if statement somewhat like below?

if (distance<= 10) strg = "far away"; 
if (distance >10 && distance =<50) strg = "within sight";
if (distance >50 && distance =<100) strg = "slow down";
if (distance >100 && distance =<200) strg = "almost there";
if (distance >200) strg = "stop!";

Yes it can.

if (distance<= 10) strg = "far away"; 
else if (distance >10 && distance <= 50) strg = "within sight";
else if (distance >50 && distance <= 100) strg = "slow down";
else if (distance >100 && distance <= 200) strg = "almost there";
else strg = "stop!";

using if every single time means if distance < 10 its still going to check all 5 every single loop, now if distance == 60 its going to check two conditionals and exit

zoomkat:
Can a logical AND be used to put the values in brackets for use with an if statement somewhat like below?

if (distance >10 && distance =<50) strg = "within sight";

if (distance >50 && distance =<100) strg = "slow down";
if (distance >100 && distance =<200) strg = "almost there";

The expression is "<= 50" not "=< 50".