Go Down

Topic: Using or statements within switch statements (Read 634 times) previous topic - next topic

Mecatronicsstudent

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;

Brad Burleson


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: [Select]
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.

Mecatronicsstudent

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

Go Up