Switch case doubt

I have a requirement to decide on multiple states based on the value of one integer variable. It can vary between 250 to 1500. And for in between this range it can have different range interpretations like 250 to 460 is Very Bad ; 461 to 600 is Bad ; 601 to 800 is Normal. And so on.

I tried out to implement this with a simple switch case as below. This works. Any other suggestions ?

int variable ; 
int caseState ; 

void setup() {
  Serial.begin(9600); 
  Serial.print("Switch Case Usage trials "); 
}

void loop() {
 
  variable += 1; 
  caseState = 0;               // Otherwise Default case is never handled. 
  Serial.println(variable); 

  if ( variable > 0 && variable < 3 ) caseState = 1 ; 
  if ( variable > 2 && variable < 5 ) caseState = 2 ; 

  switch (caseState) {

    case 1:
    Serial.println( "Variable is either 1 or 2 "); 
    break; 

    case 2:
    Serial.println( "Variable is either 3 or 4"); 
    break; 

    default :
    Serial.println( "Default case !");
    break; 
    
  }

  if ( variable > 5 ) variable = 0;

  delay(1000); 

}

Try this

byte state;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int x = 0; x < 12; x++)
  {
    printRange(x);
  }
}

void loop()
{
}

void printRange(int state)
{
  Serial.print(state);
  Serial.print("\t");
  switch (state)
  {
    case 0 ... 2:
      Serial.println("0 to 2");
      break;
    case 3 ... 4:
      Serial.println("3 to 4");
      break;
    case 5 ... 7:
      Serial.println("5 to 7");
      break;
    case 8 ... 10:
      Serial.println("8 to 10");
      break;
    default:
      Serial.println("out of range");
      break;
  }
}
1 Like

I do not understand what you trying to achive.
Can you give an example where all numbers and number-areas (250...400) are given and then describe in normal words what you want the program to do?

best regards Stefan

@Ardubit , you don't need to preprocess the value like in your example. The case part of the switch statement can handle a range of values as shown in the example by @UKHeliBob

Assuming all of the ranges are contiguous, you can use a series of 'if' statements:

  if (value < 250)
    result = INVALID;
  else
  if (value < 461)
    result = VeryBad;
  else
  if (value < 601)
    result = Bad;
  else
  if (value < 801)
    result = Normal;

The same kind of thin can be done in descending order if that makes more sense.

1 Like

@Helibob

You are bang on !! Yes this is the exact method I was looking for. Thank you so much !

( Sorry about this late a response - earlier the response notification used to be very good from Arduino Forum. But now its not so - maybe there is some issue with my mail client. I just came here to check and saw your reply and other replies too. )

To be honest since I did not know the method as suggested by Helibob, this is exactly what I did and got it going ! Did not post as I thought this if...elseIf is not a very pro solution :grinning:

Of course this method of single check is difficult to follow .... in your example a value of 200 is TRUE for all cases and so I used the if ( X > Y && X < Z ) format. This is always safe as it denotes a well defined range.

Thanks !!