Switch Case with a range?

Could I turn this to a switch case

    // Channel 5 is (0-255)  0-5=off, 6-10=runR, 11-15=runG, 16-20=runBE, 21-25=runW
    // 26-30=runFR, 31-35=runFG, 36-40=runFB, 41-45=runFW, 46-50=runRGB, 51-55=runFRGB, 56-60-runFRGBW, 61-65=runFADE    

    if (DMXSerial.read(FIVE) >= 0 && DMXSerial.read(FIVE) <= 5 ){
      runBlack();
    }
    else if (DMXSerial.read(FIVE) >= 6 && DMXSerial.read(FIVE) <= 10 ){
      runR();
    } 
//ECT.............

Maybe turn that to something like this

int var = DMXSerial.read(FIVE);
    
    switch (var) {
    case (0 to 5):
      runBlack();
      break;
    case (6 to 10):
      runR();
      break;
    case (11 to 15):
      runG();
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
      break;
    }
case 0...5:

AWOL:

case 0...5:

I get this
error: too many decimal points in number

This compiles

case 0 || 1 || 2 || 3 || 4 || 5:
runBlack;
break;

Would that be Ok or is there a better way?

How about this?

    switch (var) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
      runBlack();
      break;
...

It compiles, but does it work?

Eliipsis is the way to go. Maybe needs some spaces 0 ... 5

Will I was hoping that it would work but I get this error from this

    int var = DMXSerial.read(FIVE);
    switch (var) {
    case 0 || 1 || 2 || 3 || 4 || 5:
      runBlack();
      break;
    case 6 || 7 || 8 || 9 || 10:
      runR();
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
      break;
    }
In function 'void loop()':
error: duplicate case value
error: previously used here

The error comes from

case 6 || 7 || 8 || 9 || 10:

chrisnet:

AWOL: case 0...5:

I get this error: too many decimal points in number

Just needs a space after the zero.

chrisnet: Will I was hoping that it would work but I get this error from this

    int var = DMXSerial.read(FIVE);
    switch (var) {
    case 0 || 1 || 2 || 3 || 4 || 5:
      runBlack();
      break;
    case 6 || 7 || 8 || 9 || 10:
      runR();
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
      break;
    }
In function 'void loop()':
error: duplicate case value
error: previously used here

The error comes from

case 6 || 7 || 8 || 9 || 10:

Yes, that's correct. Think case true:

AWOL: It compiles, but does it work?

Eliipsis is the way to go. Maybe needs some spaces 0 ... 5

Thanks that did work

int var = DMXSerial.read(FIVE);
    switch (var) {
    case 0 ... 5:
      runBlack();
      break;
    case 6 ... 10:
      runR();
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
      break;
    }

AWOL: It compiles, but does it work?

Eliipsis is the way to go. Maybe needs some spaces 0 ... 5

Spaces are required to dis-ambiguate anyway:

1...3 could conceivably be lexed as "1.", ".", ".3" or "1", "...", "3".

However the lexical analyser is simple and just chomps "."'s. Then the numerical token is checked for validity. It takes whitespace or an operator (other than "." or "...") to terminate a number.

The only official C way is

case 1:
case 2:
case 3:
/*...*/

And

 3 || 4 || 5 || 6

won't work. It will yield 1. If there is a zero in there it will always be zero.

The 1 .. 5 syntax is a gcc extension. Since you will probably always use gcc for this it should always work, but if avr should switch to clang or something, it might break in the future.

KeithRB: The only official C way is

case 1:
case 2:
case 3:
/*...*/

And

 3 || 4 || 5 || 6

won't work. It will yield 1. If there is a zero in there it will always be zero.

The 1 .. 5 syntax is a gcc extension. Since you will probably always use gcc for this it should always work, but if avr should switch to clang or something, it might break in the future.

KeithRB, Thanks for the information on this! I love to know not just what works but also why. Does the other gcc extensions "http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html" work as well?

Nested functions didn't work last time I tried.

MarkT:

AWOL: It compiles, but does it work?

Eliipsis is the way to go. Maybe needs some spaces 0 ... 5

Spaces are required to dis-ambiguate anyway:

1...3 could conceivably be lexed as "1.", ".", ".3" or "1", "...", "3".

However the lexical analyser is simple and just chomps "."'s. Then the numerical token is checked for validity. It takes whitespace or an operator (other than "." or "...") to terminate a number.

AFIK, the C lexical parser operates on the "maximum munch" principal and tries to use up as many tokens as it can. Therefore, it will parse "1." before "1...5" even if it generates an error.

I don’t really know about extensions, I try to stay away from them. Except I am guilty of using the 0b0101 extension, since it seems so … obvious.

switch(temp) //Poner aqui vaiable afectada por pulsos (flujo) y temperatura { case 0 ... 10: consKp=10; consKi=10; consKd=10; break;

case 11 ... 20: consKp=20; consKi=20; consKd=20; break;

default: consKp=5; consKi=5; consKd=5; break; }