Pages: [1]   Go Down
Author Topic: Using or statements within switch statements  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been writing code for a Morse code flasher. In on section of the code I have it so that the serial monitor will take a capital or lowercase letter. The problem is that it gives me an error of duplicate case value. Is it not possible to use or statements within a switch statement like this. 

case
      'a'|| 'A' :
      Serial.println ('a');
      dot();
      dash();
      letterspace();
      break;
Logged

Salem, Oregon
Offline Offline
Sr. Member
****
Karma: 7
Posts: 310
Friends don't let friends use Strings
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that it gives me an error of duplicate case value. Is it not possible to use or statements within a switch statement like this. 


case
      'a'|| 'A' :
      Serial.println ('a');
      dot();
      dash();
      letterspace();
      break;

What you want to do is something like this

Code:
switch (var) {
    case 'a':
    case 'A':
      //do something when var equals a/A
      break;
    case 'b':
    case 'B':
      //do something when var equals b/B
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
  }

Of course what you should really do is study the code that was posted in your other thread by liudr. 

Hope this helps,

Brad.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks. I will look at the other code as well.
Logged

Pages: [1]   Go Up
Jump to: