Go Down

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

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.


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy