user51895: Following code is to control two LEDs for on and off by two dedicated push buttons. I have used if() for each condition and I was not comfortable writing this code, as I could feel, it can be better with switch...case, because if we increase more buttons, it will be a messy solution for that. However, I couldn't get optimized switch...case for it. Can some one suggest the nice switch transformation for this if().
int buttonState1 = 0; // variale for reading pushbutton1 status
int buttonState2 = 0;  // variable to reading push button2 status 
//set pin numbers
int button1 = 2; // push button no. 1
int button2 =3;  // push button no. 2
int Led1 = 4; // led 1
int Led2 =5;  // led 2

void setup() {
  Serial.begin(9600); // serial begins at 9600
  // defining pin modes for led and buttons 
  pinMode(button1, INPUT); 
  pinMode(button2, INPUT); 
  pinMode (Led1, OUTPUT);
  pinMode (Led2, OUTPUT);  
};

void loop() {
 buttonState1 = digitalRead(button1); // reading pushbutton 1 in the variable buttonstate1 
 buttonState2 = digitalRead(button2); // reading pushbutton 2 in the variable buttonstate2 
 // condintion for led 1 ON, if pushbutton1 is pressed
 if (buttonState1 == HIGH) {            
  digitalWrite(Led1, HIGH);     
  }
  // condition for led2 ON, if pushbutton2 is pressed.
 if (buttonState2 ==HIGH){

  digitalWrite(Led2, HIGH);

  }
  // both led are off if no push button is pressed
  else{
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
  }

};


user49519: A switch case does not seem like it would be an effective way of controlling your LEDs. It would make more sense (and would probably be easier) to create a function instead to control the LEDs. 
void buttonToggle(int buttonPin, int ledPin) {
  int buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

You would then need to replace your if statement with calls to the function (EX buttonToggle(Button1, Led1);

user37735: If you want to use a dedicated push button for every led, then it is not handy to use a switch .. case statement. This is because first you have to construct the switch-variable based on the readings of the switches before you can use it (the switch variable) for de case statements.
When you combine the reading of the switches with the setting of the leds you get a nice Loop-section:
    void loop() {
      digitalWrite(Led1, digitalRead(button1));
      digitalWrite(Led2, digitalRead(button2));
      digitalWrite(Led3, digitalRead(button3));
      digitalWrite(Led4, digitalRead(button4));
      digitalWrite(Led5, digitalRead(button5));
      // and so on
    }


51895: <p>Following code is to control two LEDs for on and off by two dedicated push buttons. I have used <code>if()</code> for each condition and I was not comfortable writing this code, as I could feel, it can be better with <code>switch...case</code>, because if we increase more buttons, it will be a messy solution for that. However, I couldn't get optimized <code>switch...case</code> for it. Can some one suggest the nice switch transformation for this <code>if()</code>.</p>

<pre><code>int buttonState1 = 0; // variale for reading pushbutton1 status
int buttonState2 = 0;  // variable to reading push button2 status 
//set pin numbers
int button1 = 2; // push button no. 1
int button2 =3;  // push button no. 2
int Led1 = 4; // led 1
int Led2 =5;  // led 2

void setup() {
  Serial.begin(9600); // serial begins at 9600
  // defining pin modes for led and buttons 
  pinMode(button1, INPUT); 
  pinMode(button2, INPUT); 
  pinMode (Led1, OUTPUT);
  pinMode (Led2, OUTPUT);  
};

void loop() {
 buttonState1 = digitalRead(button1); // reading pushbutton 1 in the variable buttonstate1 
 buttonState2 = digitalRead(button2); // reading pushbutton 2 in the variable buttonstate2 
 // condintion for led 1 ON, if pushbutton1 is pressed
 if (buttonState1 == HIGH) {            
  digitalWrite(Led1, HIGH);     
  }
  // condition for led2 ON, if pushbutton2 is pressed.
 if (buttonState2 ==HIGH){

  digitalWrite(Led2, HIGH);

  }
  // both led are off if no push button is pressed
  else{
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
  }

};
</code></pre>
 49519: <p>A switch case does not seem like it would be an effective way of controlling your LEDs. It would make more sense (and would probably be easier) to create a function instead to control the LEDs. </p>

<pre><code>void buttonToggle(int buttonPin, int ledPin) {
  int buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}
</code></pre>

<p>You would then need to replace your <code>if</code> statement with calls to the function (EX <code>buttonToggle(Button1, Led1);</code></p>
 37735: <p>If you want to use a dedicated push button for every led, then it is not handy to use a switch .. case statement. This is because first you have to construct the switch-variable based on the readings of the switches before you can use it (the switch variable) for de case statements.</p>

<p>When you combine the reading of the switches with the setting of the leds you get a nice Loop-section:</p>

<pre><code>    void loop() {
      digitalWrite(Led1, digitalRead(button1));
      digitalWrite(Led2, digitalRead(button2));
      digitalWrite(Led3, digitalRead(button3));
      digitalWrite(Led4, digitalRead(button4));
      digitalWrite(Led5, digitalRead(button5));
      // and so on
    }
</code></pre>
