switch... case 0, 2, 4, 6, 8:

Good morning

I would like to know if is possible to do the following or near?

switch (MenuActual){
case 0, 2, 4, 6, 8:
//do something

case 1, 3, 5, 7, 9:
//do something

}

Thanks on advance
Best regards

Pedro Ferrer

No, I think you can only have value.

If this is exactly what you want to do then,

if (menuActual % 2)
{
  // do even stuff
}
else
{
  // do odd stuff
}

Thanks Si

On VB I can have it...

Select Case MenuActual
Case 0, 2, 4, 6, 8
//do something
Case 1, 3, 5, 7, 9
//do something
End Select

Thanks on advance
Best regards

Pedro Ferrer

Sadly, thats one nice feature of VB that is not in C.

You'll probably end up using ifs and else ifs.

Try this:

switch (MenuActual)
{
     case 0:
      do1();
      break;
     case 2:
      do1();
      break;
     //... 
           
     case 1:
      do2();
      break;
     case 3:
      do2();
      break;
     //... 
}

Good morning

Thanks Webmeister.

Is what I have at the moment.
I've just thought that exist a better way to do it...
It seems that unfortunately... not

Thanks to you all

Best regards
Pedro Ferrer

switch (MenuActual){
     case 0:
     case 2:
     case 4:
     case 6:
     case 8:
           //do something
           break;
}

Good morning

Groove, please explain me:

switch (MenuActual){
case 0:
case 2:
case 4:
case 6:
case 8:
//do something (action 1)
break;

case 1:
case 3:
case 5:
case 7:
case 9:
//do something (action 2)
break;
}

It will work?

Thanks on advance
Best regards

Pedro Ferrer

Yes, it will work.
Any "case" that doesn't have "break" will fall-through to the next case.

Thanks.
I'll try later at night.

Thanks once again
Best regards

Pedro Ferrer

:cry:

What's up deSilva?

Best regards
Pedro Ferrer

Difficult to say...
Why is C/C++ so primitive?
Why took it so long you got a good answer?
Why couldn't you find out yourself?

Oh, forget it! I'm sometimes in this mood....

What's up deSilva?

I just think s/he's trying to bump her/his post count.

If you think of C (or C++) as a high-level assembly language (which is what C was designed as), you won't ever make the mistake of thinking it "primitive".

if (c & 1) {
   // c is odd
} else {
  // c is even
}

Or perhaps:

if (c >= 0) && (c <=9) {
  if (c % 2) {
    // c is even
  } else {
    // c is odd
  }
}

I just think s/he's trying to bump her/his post count

If you really think this, I am speechless :o

as if

anyone

would do such a thing :wink:

For weeks now I try to reduce the number of my postings.. I make progress.. Good hope to bring it down further..