There is no "better" (if/then // switch/case). It all depends on the program. For example, your program, no if/then/switch/case...
#define ledpinRed 13
#define ledpinGrn 12
#define ledpinBlu 14
#define buttonRed 2
#define buttonGrn 4
#define buttonBlu 5
int pin[3][2] = { // array of "int"s called "pin" with three (color) rows and two (pins) columns
{ledpinRed, buttonRed}, // pin[0][0], pin [0][1]
{ledpinGrn, buttonGrn}, // pin[1][0], pin [1][1]
{ledpinBlu, buttonBlu} // pin[2][0], pin [2][1]
};
void setup() {
for (int i = 0; i < 3; i++) { // three colors
pinMode(pin[i][0], OUTPUT); // LED pins
pinMode(pin[i][1], INPUT_PULLUP); // button pins
}
}
void loop() {
for (int i = 0; i < 3; i++) {
digitalWrite(pin[i][0], !digitalRead(pin[i][1])); // read a pin, light an LED
}
}
diagram.json tab for wokwi.com
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -19.7, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -51.6,
"left": -92.2,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -51.6,
"left": -73,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -51.6,
"left": -53.8,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -41.8,
"left": 115.2,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": -89.8,
"left": 115.2,
"attrs": { "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": -137.8,
"left": 115.2,
"attrs": { "color": "blue" }
}
],
"connections": [
[ "nano:GND.2", "led3:C", "black", [ "v-9.6", "h-163.6" ] ],
[ "led3:C", "led2:C", "black", [ "v9.6", "h-9.2" ] ],
[ "led2:C", "led1:C", "black", [ "v9.6", "h-18.8" ] ],
[ "led1:A", "nano:13", "green", [ "v86.4", "h96" ] ],
[ "led2:A", "nano:12", "green", [ "v28.8", "h76.8" ] ],
[ "led3:A", "nano:A0", "green", [ "v96", "h86.4" ] ],
[ "nano:GND.2", "btn1:2.l", "black", [ "v0" ] ],
[ "nano:GND.2", "btn2:2.l", "black", [ "v0" ] ],
[ "nano:GND.2", "btn3:2.l", "black", [ "v0" ] ],
[ "nano:2", "btn1:1.l", "green", [ "v0" ] ],
[ "nano:4", "btn2:1.l", "green", [ "v0" ] ],
[ "nano:5", "btn3:1.l", "green", [ "v0" ] ]
],
"dependencies": {}
}
Keep an open mind when solving problems. Sometimes a different approach is all you need.