switch case trouble

Hai guys, i am trying to write a simple program which reads from the serial monitor and executes a function accoring to the given input. here's my program..

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly: 
  if(Serial.available()>0){
   // int y = ;
    switch(Serial.read()){
      case 10 :
      Serial.print("10");
      break;
      case 20:
      Serial.print("20");
      break;
      case 220:
      Serial.print("220");
      break;
      case 30:
      Serial.print("30");
      break;
      default:
      Serial.println("Nothing selected..!");
      break;
    }//end of switch
    
  }
}

there is no error, but not working as i need.

What are you getting for Serial.read(), an int or a char? You should cast it as an int, like so.

switch( (int)Serial.read() ){

Better yet, take it out of there and assign it to a variable. That way you can actually see what your getting when you use the serial monitor.

but not working as i need

Try realigning your expectations.
Or, tell us what they are, and we'll realign them for you.

What you posted is probably a test.
But you can do that test a lot simpler.
If you just do a serial.println () (< place a variable or value between the braces), you will see what happens and probably why you aren't getting back what you are expecting.
The println adds a cr/lf so the next value will be printed at a new line (i think that is what you want anyway).

Simplify as suggested:

byte y = Serial.read() ;
Serial.println(y, HEX); // what came in?
switch(y){

Ajay_Pyaraka:
Hai guys, i am trying to write a simple program which reads from the serial monitor and executes a function accoring to the given input.

It's impossible that you write the character 10 on monitor :grin:

if(Serial.available()>0){
   // int y = ;
    switch(Serial.read()){
case 10:

When you write on monitor, example 10 you not write value 10 but two char, '1' and after '0'.
Serial.read() read only one char at a time. '1' and after '0' or also 49 (ascii of '1') and 48 (ascii of '0')
If you write 'A' on monitor you send ascii value 65 ('A'=65) but ascii char 10 is not writable using keyboard.

It's impossible that you write the character 10 on monitor

Rubbish. It's a line-feed

Ajay_Pyaraka:
there is no error, but not working as i need.

What do you need?

AWOL:
Rubbish. It's a line-feed

The semantics of Enter versus Return in a Windows environment are application-specific and pretty inconsistent. The only way I know of to send a line-feed using the Arduino Serial monitor (without also sending other characters) is to select Newline as the line ending mode and then hit Enter or Return. That doesn't simply send the character you typed, it recognises the input character as being an end-of-line and sends the currently-configured end-of-line sequence.

Still you can send this to your serial monitor or whatever you've got connected to serial.