Could someone explain the differences in using Switch-Case vs IF statements ?

Originally started using the switch-case for selection of IR remote and later as a comparison changed the body to use IF statements. With that I found that the routine I had put together for changing the brightness no longer worked with the IF statements, only lightly flickered…but did change brightness levels using switch-case. I have pasted both codes below and would like if possible the pros and cons of using either situation as I would prefer to use the correct code for the task at hand.

// Switch-case CODE
#include "IRremote.h"
#include <Adafruit_NeoPixel.h>
#define PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ800);

int d = 4;
int TOTAL_LEDS = 150;
int pos = 0, dir = 1;
int j = 0;
int brit = 100;
int direction=1;
int receiver = 3;

IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

void setup()  
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  Serial.println("IR Receiver Raw Data + Button Decode Test");
}

void loop()  
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    irrecv.resume(); // receive the next value
  } 
  switch(results.value)
  {
  case 0xFF3AC5: 
    Serial.println("Brightness Down");// lowest brightness level
    strip.setBrightness(brit/4);
    strip.show();
    break;
  case 0xFFBA45: 
    Serial.println("Brightness Up");// mid-brightness level
    strip.setBrightness(brit);
    strip.begin();
    strip.show(); 
    break;
  case 0xFF827D: 
    Serial.println("Play");// high brightness level
    strip.setBrightness(brit*2);
    strip.begin();
    strip.show(); 
    break;
  case 0xFF1AE5:  
    Serial.println("RED");
    RedColor();
    break;
  case 0xFF9A65: 
    Serial.println("GREEN");
    GreenColor();
    break; 
  default:
    Serial.println("Waiting");
  }
  delay(d*50); 
}

void RedColor()
{
  strip.begin();
  strip.show(); 
  for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++)
  {
    strip.setPixelColor(ledNumber, 128,0,0);
  }
}

void GreenColor()
{
  strip.begin();
  strip.show(); 
  int TOTAL_LEDS = 150;
  for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++)
  {
    strip.setPixelColor(ledNumber, 0,128,0);
  }
}
// IF statements CODE
#include "IRremote.h"
#include <Adafruit_NeoPixel.h>
#define PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ800);

#define BrightnessDownButton 0xFF3AC5
#define BrightnessUpButton 0xFFBA45
#define PlayButton 0xFF827D
#define RedButton 0xFF1AE5
#define GreenButton 0xFF9A65

int d = 4;
int TOTAL_LEDS = 150;
int pos = 0, dir = 1;
int j = 0;
int brit = 100;
int direction=1;
int receiver = 3;

IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

void setup()  
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  Serial.println("IR Receiver Raw Data + Button Decode Test");
}

void loop()  
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    irrecv.resume(); // receive the next value   
  } 
  Serial.println(results.value, HEX);
  if (results.value == BrightnessDownButton)
  {
    strip.setBrightness(35);
    Serial.println("Brightness Down");   
  }
  if (results.value == BrightnessUpButton)
  {
    Serial.println("Brightness Up");
  }
  strip.setBrightness(110);
  if (results.value == PlayButton)
  {
    strip.setBrightness(240);
    Serial.println("Play"); 
  }
  if (results.value == RedButton)
  {
    RedColor();
    Serial.println("Red");
  }
  if (results.value == GreenButton)
  {
    GreenColor();
    Serial.println("Green"); 
  } 
  delay(d*50);
}


void RedColor()
{
  strip.begin();
  strip.show(); 
  for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++)
  {
    strip.setPixelColor(ledNumber, 128,0,0);
  }
}

void GreenColor()
{
  strip.begin();
  strip.show(); 
  int TOTAL_LEDS = 150;
  for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++)
  {
    strip.setPixelColor(ledNumber, 0,128,0);
  }
}

What happens in the if version if you lengthen the delay() at the bottom?

Serial.println("Brightness Up");// mid-brightness level
strip.setBrightness(brit);

where is this code in your if(s) ? AFTER you set brightness? I see it only in setting colors

** strip.begin();**
** strip.show();**

break;

would like if possible the pros and cons of using either situation as I would prefer to use the correct code for the task at hand.

Neither is right or wrong. It is a matter of perdonal preference as long as the code works.

Personally I prefer switch/case as I find it easier to read and maintain but it does have the disadvantage of not being able to use complex expressions in the case statements so sometimes it is easier to use if/else.

Horses for courses.

Thanks for the replies.

“What happens in the if version if you lengthen the delay() at the bottom?”
Changing the delay from d50 to d500 onlu slows the flickering but does not change the brightness levels ( int d = 4; )

"Serial.println(“Brightness Up”);// mid-brightness level
strip.setBrightness(brit);

where is this code in your if(s) ? AFTER you set brightness? I see it only in setting colors

strip.begin();
strip.show(); "

… should have updated the IF statement code completely and have done so, posted below…wish that it was the fix but the brightness level change (desire to do this global level using int brit) only seems to work when using the switch-case.

#include "IRremote.h"
#include <Adafruit_NeoPixel.h>
#define PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ800);

#define BrightnessDownButton 0xFF3AC5
#define BrightnessUpButton 0xFFBA45
#define PlayButton 0xFF827D
#define RedButton 0xFF1AE5
#define GreenButton 0xFF9A65
#define DIY2Button 0xFFB04F
#define DIY5Button 0xFF906F
int d = 4;
int TOTAL_LEDS = 150;
int pos = 0, dir = 1;
int j = 0;
int brit = 100;
int direction=1;
int receiver = 3;

IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

void setup()  
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  Serial.println("IR Receiver Raw Data + Button Decode Test");
}

void loop()  
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    irrecv.resume(); // receive the next value   
  } 
  Serial.println(results.value, HEX);
  if (results.value == BrightnessDownButton)
  {
    strip.setBrightness(brit/4);
    strip.begin();
    strip.show(); 
    Serial.println("Brightness Down");   
  }
  if (results.value == BrightnessUpButton)
  {
    Serial.println("Brightness Up");
  }
  strip.setBrightness(brit);
  strip.begin();
  strip.show(); 
  if (results.value == PlayButton)
  {
    strip.setBrightness(brit*2);
    strip.begin();
    strip.show(); 
    Serial.println("Play"); 
  }
  if (results.value == RedButton)
  {
    RedColor();
    Serial.println("Red");
  }
  if (results.value == GreenButton)
  {
    GreenColor();
    Serial.println("Green"); 
  } 
  if (results.value == DIY2Button)
  {
    DIY2();
    Serial.println("DIY2"); 
  }
  if (results.value == DIY5Button)
  {
    DIY5();
    Serial.println("DIY5"); 
  }
  delay(d*500);
}


void RedColor()
{
  strip.begin();
  strip.show(); 
  for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++)
  {
    strip.setPixelColor(ledNumber, 128,0,0);
  }
}

void GreenColor()
{
  strip.begin();
  strip.show(); 
  int TOTAL_LEDS = 150;
  for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++)
  {
    strip.setPixelColor(ledNumber, 0,128,0);
  }
}
void DIY2(){

  strip.begin();
  strip.show(); 
  strip.setPixelColor(pos - 6, 0x000011); 
  strip.setPixelColor(pos - 5, 0x000088); 
  strip.setPixelColor(pos - 4, 0x0000FF); 
  strip.setPixelColor(pos - 3, 0x0000FF); 
  strip.setPixelColor(pos - 2, 0XCCCCCC); 
  strip.setPixelColor(pos - 1, 0XFFFFFF); 
  strip.setPixelColor(pos    , 0xFFFFFF); 
  strip.setPixelColor(pos + 1, 0xFFFFFF); 
  strip.setPixelColor(pos + 2, 0xCCCCCC); 
  strip.setPixelColor(pos + 3, 0xFF0000); 
  strip.setPixelColor(pos + 4, 0xFF0000); 
  strip.setPixelColor(pos + 5, 0x880000);
  strip.setPixelColor(pos + 6, 0x110000); 

  strip.show();

  for(j=-2; j<= 2; j++) strip.setPixelColor(pos+j, 0);

  pos += dir;
  if(pos < 0) {
    pos = 1;
    dir = -dir;
  } 
  else if(pos >= strip.numPixels())
  {
    pos = strip.numPixels() - 2;
    dir = -dir;
  }
}

void DIY5(){

  strip.begin();
  strip.show();
  strip.setPixelColor(pos - 5,   25, 5, 0); 
  strip.setPixelColor(pos - 4,   25, 5, 0); 
  strip.setPixelColor(pos - 3,   25, 5, 0); 
  strip.setPixelColor(pos - 2, 110, 22, 5);
  strip.setPixelColor(pos - 1, 220, 50, 8); 
  strip.setPixelColor(pos    , 220, 50, 8); 
  strip.setPixelColor(pos + 1, 220, 50, 8); 
  strip.setPixelColor(pos + 2, 110, 22, 5); 
  strip.setPixelColor(pos + 3,   25, 5, 0); 
  strip.setPixelColor(pos + 4,   25, 5, 0); 
  strip.setPixelColor(pos + 5,   25, 5, 0); 
  strip.show();

  for(j=-2; j<= 2; j++) strip.setPixelColor(pos+j, 0); 
  pos += dir;
  if(pos < 0)
  {
    pos = 1;
    dir = -dir;
  } 
  else if(pos >= strip.numPixels())
  {
    pos = strip.numPixels() - 2;
    dir = -dir;
  }
}

In this section of code:

  if (results.value == BrightnessDownButton)
  {
    strip.setBrightness(brit/4);
    strip.begin();
    strip.show(); 
    Serial.println("Brightness Down");   
  }
  if (results.value == BrightnessUpButton)
  {
    Serial.println("Brightness Up");
  }
  strip.setBrightness(brit);
  strip.begin();
  strip.show();

You check for the Brightness down button and if detected, set the brightness to a quarter of the max. However, immediately afterwards, you unconditionally set it back to full (last three lines of the above snippet). I'd guess you intended those three lines to be inside the braces with the "Brightness Up" message.

Programmer error. Your wrote:

  if (results.value == BrightnessUpButton)
  {
    Serial.println("Brightness Up");
  }
  strip.setBrightness(110);

When you probably meant:

  if (results.value == BrightnessUpButton)
  {
    Serial.println("Brightness Up");
    strip.setBrightness(110); 
  }