Go Down

Topic: switch... case 0, 2, 4, 6, 8: (Read 2 times) previous topic - next topic

Pedro Ferrer

Good morning

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

Quote
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

Si

No, I think you can only have value.

If this is exactly what you want to do then,

Code: [Select]

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


--
My New Arduino Book: http://www.arduinobook.com

Pedro Ferrer

Thanks Si

On VB I can have it...

Quote
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

Si

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

You'll probably end up using ifs and else ifs.
--
My New Arduino Book: http://www.arduinobook.com

Webmeister

Try this:

Code: [Select]

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

Pedro Ferrer

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

Groove

Code: [Select]
switch (MenuActual){
    case 0:
    case 2:
    case 4:
    case 6:
    case 8:
          //do something
          break;
}
Per Arduino ad Astra

Pedro Ferrer

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

Groove

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

Pedro Ferrer

Thanks.
I'll try later at night.

Thanks once again
Best regards

Pedro Ferrer

deSilva


Pedro Ferrer

What's up deSilva?

Best regards
Pedro Ferrer

deSilva

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

Groove

#13
Sep 16, 2010, 01:21 pm Last Edit: Sep 16, 2010, 01:24 pm by GrooveFlotilla Reason: 1
Quote
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".
Per Arduino ad Astra

westfw

Code: [Select]
if (c & 1) {
  // c is odd
} else {
 // c is even
}


Or perhaps:
Code: [Select]

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

Go Up