Advice on 'Switch Case' or 'If Else' usage please

Hey all

As I read through example code in my efforts to learn Arduino and Python, I have today stumbled across a new command that I quite like the use of.

The 'Switch... Case' command!

Can some experienced coders please enlighten me as to which of the commands 'Switch... Case' or 'If... Else' I should use in any given situation?

Ta muchly...

It is mostly a matter of taste.

I like switch/case because to me it isolates the code blocks better but it only works where the cases can be resolved to an integer, but that does lend itself to using constants or an enum for the case values.

if/else can use compound values in the test conditions which make it more versatile, but I often find it more difficult to understand when used in other people's code or mine written some time ago.

Whatever looks good to you. There use to be some arcane reasons to choose one or the other, but compilers have gotten so good, and machine resources so much more expansive that it is really personal preference.

I like switch case when I want to easily change/swap cases and see at a glance which cases are handled:

switch (range) {

	case 0:
	case 1:
		sprintf(sbuf, "%6.4f mW", mW);
		break;

	case 2:
	case 3:
		sprintf(sbuf, "%6.3f mW", mW);
		break;

	case 4:
	case 5:
	case 6:
	default:
		sprintf(sbuf, "%6.2f mW", mW);
		break;

	}

I agree with Bob. I think the switch/case is easier to read and understand. Also, the code generated by the compiler is often a jump table and is usually pretty efficient. Finally, I like to use the default case in an error-trapping kind of situation, like:

switch (whichDay) {   // 1 - 7
   case SUNDAY:
      doSunday();
      break;
   case MONDAY:
      doMonday();
      break;

// ...and so on...

   case SATURDAY:
      doSaturday();
      break;
   default:
      Serial.print("I shouldn't be here. whichDay = ");
      Serial.println(whichDay);
      errorflag = true;
      breakl;
}

Sometimes this kind of defensive coding pays off.

When it comes to 'one' integer variable, I use Switch/Case.

.

Ha! Great responses folks... thank you very much. Seems like a Friday evening is awash with Arduino and coding geeks like me!

Appreciated.

Now then... which day is it...? :slight_smile:

Also, no matter whether you use if or switch, if you find you have too much* code inside the if or the case you probably want to put that in a function - even if this is the only place that it is called.

  • I originally said "two pages of" but decided that a judgment call was better than an arbitrary rule.

KeithRB:
Also, no matter whether you use if or switch, if you find you have too much* code inside the if or the case you probably want to put that in a function - even if this is the only place that it is called.

  • I originally said "two pages of" but decided that a judgment call was better than an arbitrary rule.

Cheers Keith... I understand the 'why' you might want to do this... but the 'how', now that's something Bart Simpson himself might barf at!! :slight_smile:

Care to elaborate for me please on how I might do that?

PS: And OMG!!!! this 5 minute posting rule!!!!! grrrrr

What a pain it is when you're genuinely trying to post and reply to legitimate threads..

I was a huge fan of switch also, much clearer to read and, when using enumeration the comiler even warn you when you don't have all possible options covered ...

until i read somewhere that every switch statement takes some amount of SRAM to store a jump table with addresses. Being short of memory already I removed all switched and replaces them with if {} else if {}.

It utterly amazes me why this jump table would be in SRAM by the way, nothing dynamic about that

theMusicMan:
Care to elaborate for me please on how I might do that?

just create a new function and copy and paste. You could even inline it so it is not an actual function call.

bascy:
until i read somewhere that every switch statement takes some amount of SRAM to store a jump table with addresses. Being short of memory already I removed all switched and replaces them with if {} else if {}.

I just coded my example using the switch/case and as a cascading if-else block. The switch/case used 2124 bytes of flash and 218 bytes of SRAM. The cascading if-else block was 2220 and exactly the same SRAM. This was with release 1.8.2 of the compiler. Maybe the code generator has changed.

econjack:
I just coded my example using the switch/case and as a cascading if-else block. The switch/case used 2124 bytes of flash and 218 bytes of SRAM. The cascading if-else block was 2220 and exactly the same SRAM. This was with release 1.8.2 of the compiler. Maybe the code generator has changed.

That is good news!
Time to update and retest

Seems to me that a state machine paradigm maps more readily to switch..case, especially if you enum the states

enum {ST_IDLE, ST_RUNNING, ST_STOPPING} state = ST_IDLE;
..
..
..

switch (state)
{
     case ST_IDLE:
          ..
          ..

     case ST_RUNNING:
          ..
          ..

     case ST_STOPPING:
          ..
          ..
}