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;
}
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!!
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
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.