Pages: [1] 2   Go Down
Author Topic: switch... case 0, 2, 4, 6, 8:  (Read 2699 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I think you can only have value.

If this is exactly what you want to do then,

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

Logged

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

CH
Offline Offline
God Member
*****
Karma: 19
Posts: 703
Book Writer "Arduino Praxiseinstieg"
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.
I'll try later at night.

Thanks once again
Best regards

Pedro Ferrer
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 :'(
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What's up deSilva?

Best regards
Pedro Ferrer
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
« Last Edit: September 16, 2010, 06:24:10 am by GrooveFlotilla » Logged

Per Arduino ad Astra

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 133
Posts: 6749
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Or perhaps:
Code:
if (c >= 0) && (c <=9) {
  if (c % 2) {
    // c is even
  } else {
    // c is odd
  }
}
Logged

Pages: [1] 2   Go Up
Jump to: