switch (char(an_Int) with case char(0)...char(127):

I'm sure this is not new but I wanted to use 'case' with ranges of byte values and
of course
case 1...6 :
is not allowed. However, I found that by converting to char the following code works as expected; or does it? Is there something lurking there that will catch me out? after several days use there doesn't seem to be a gremlin.
Thanks
Dave

int inint;
int myinbufctr;
char myinbuf[6];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  myinbufctr = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    myinbuf[myinbufctr] = Serial.read();
    if (myinbuf[myinbufctr] == 10) {
      myinbuf[myinbufctr] = '\0';
      myinbufctr = 0;
      inint = atoi(myinbuf);
      Serial.println(inint, HEX);
      switch (char(inint)) {
        case char(128)...char(255) :
          // do something
          Serial.println("128...255");
          break;
        case char(0)...char(127) :
          // do something else
          Serial.println("0...127");
          break;
      }
    }
    else {
      myinbufctr++;
    }
  }
}
      switch (char(inint)) {

Why are you creating a char from the int?

switch(inint)
{
   case 128...255:

Hahaha thats some scary stuff right there, try this:

switch( range )
{
  case 0:
  case 1:
  case 2:
  case 3:
{
  // executes for the range 0-3 inclussive
}
break;

case 5:
{
  //only executes when range is 5
}
break;
}

PaulS:

      switch (char(inint)) {

Why are you creating a char from the int?

switch(inint)

{
  case 128...255:

Because as I said in the original post case statements of the type you quote only work for chars not ints, this is C not Pascal.
Dave

Ellipses work with int if you put a space before and after the ellipses.

int num = 3;

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    switch(num)
    {
    case 1 ... 5:
        Serial.println("hit 1 to 5");
        break;
    case 6 ... 10:
        Serial.println("hit 6 to 10");
        break;
    }
    delay(5000);
}

tammytam:
Hahaha thats some scary stuff right there, try this:

switch( range )

{
 case 0:
 case 1:
 case 2:
 case 3:
{
 // executes for the range 0-3 inclussive
}
break;

case 5:
{
 //only executes when range is 5
}
break;
}

You want to do that for more than 4 consecutive numbers? you'ld be their all day typing. Say for example you want to split them into groups of 10 consecutive numbers, all the way from 0 to 255.
Dave

groundfungus:
Ellipses work with int if you put a space before and after the ellipses.

int num = 3;

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    switch(num)
    {
    case 1 ... 5:
        Serial.println("hit 1 to 5");
        break;
    case 6 ... 10:
        Serial.println("hit 6 to 10");
        break;
    }
    delay(5000);
}

Well how about that!!!! is that just a quirk of the arduino C++ or is it standard?
I had a conversation some weeks ago on here re case statements and no-one else mentioned that little quirk!!
Dave
BTW are there any other statements that are 'space' sensitive?

Quite frankly I wouldn't consider a switch statement for this purpose, at least not on its own, either use if()else, or map it if each bin is equal.

ie

int new_val = map(old_val, 0, 255, 0, 10);
switch(new_val)
{
  case 0:
  break;
// All the way down to ...
case 10:
break;
}

of course
case 1...6 :
is not allowed.

but
case 1 ... 6: is allowed

DavidI:
Well how about that!!!! is that just a quirk of the arduino C++ or is it standard?
I had a conversation some weeks ago on here re case statements and no-one else mentioned that little quirk!!
Dave
BTW are there any other statements that are 'space' sensitive?

It is a gcc extension.

Many statements are space sensitive:
k=i+++++g;

Check out "maximal munch"

KeithRB:
It is a gcc extension.

Many statements are space sensitive:
k=i+++++g;

Check out "maximal munch"

Thanks, I will and before I have a heart attack what is the above doing? incrementing i by 1 five times then adding g and leaving the result in k.
I still hate C :slight_smile:

UKHeliBob:
but
case 1 ... 6: is allowed

Seems there is a little inconsistency here in the compiler:
1...6: doesn't work
1 ... 6: does
but so does
'1'...'6'
and char(1)...char(6); does as well

and I'd bet on spaces before and after the ellipses would also work in the last two examples.
so only in the single case of pure numbers are spaces required. But might it be that for the compiler the presence of a non numeral before and after the ellipses is sufficient?

We've moved away from my original query of the possibility of gremlins in my coding but it's been fun
Dave

Seems there is a little inconsistency here in the compiler:
1...6: doesn't work
1 ... 6: does

In the first case, is that 1. .. 6? Or, is it 1 ... 6?

groundfungus:
Ellipses work with int if you put a space before and after the ellipses.

At a guess, without the space the compiler thinks you are trying to write a floating-point number

At a guess, without the space the compiler thinks you are trying to write a floating-point number

error: too many decimal points in number

PaulMurrayCbr:
At a guess, without the space the compiler thinks you are trying to write a floating-point number

Without the spaces you couldn't tell the difference between 1...6 and (1.).(.6)

MarkT:
Without the spaces you couldn't tell the difference between 1...6 and (1.).(.6)

But the first has a meaning known to the compiler, the 2nd? But having just read up on maximal munch it seems the compilers don't use logic :-).

I guess the question is, should the compiler assume you know what you are doing and try every possible permutation of the tokens to find something that works, or just use a best guess, and if it is not understandable throw an error?

What if there are two variations that work? Which should the compiler use?

Without the spaces you couldn't tell the difference between 1...6 and (1.).(.6)

But switch/case is only valid for integers anyway, isn't it ? If so, 1. and .6 would be invalid.

Who knows with gcc. 8^)