I'm not sure I understand the difference between an if block and switch/select block. I mean I use them all the time. But It's not clear to me when the switch block should be applied and when no to. Since, a select block can be expanded into if block. So the only real advantage I can think of for using the select block is for human readability.
The answer will vary a bit depending on the language. For instance, in C and C++, switch
(select) statements can get turned into fairly efficient jump tables by the compiler, partially because they're more restrictive than if
statement conditions. (Although to be fair modern compilers are pretty darned good at optimizing all sorts of stuff.) This may not be true of all languages/compilers (I seem to recall VB6 basically treated Select Case
as a series of If/ElseIf
statements.)
Readability is definitely a factor: Using a switch
(or Select Case
in VB) tells anyone following you that all of the branches below branch on a common condition, which is quite useful.
Maintainability (related to, but different from, readability) is a factor as well. If you change the one thing being branched on in a switch
, you're done; if you have a long series of if
statements, it's easy to miss one (or more) out.
Depending on the langauge I suppose, but let's assume you're talking about C. In C, the switch-block will translate into a jump table, so you will, based on the value, jump directly to the part you want to execute, whereas a if-block you can make arbitrary comparisions, and would have to compare it to every possible value to find the branch to execute.
Note that the switch can only use set values (the jump table is created at compile time), whereas an if block can do less-than/greater-than comparisons, or comparisons to a variable.
You would want to use an if block if you want to compare more than one item. You could use the if/elseif/else. If you wanted to react to the value of one variable you would use the switch/select block.
if (today is monday) {
it is moday.
} else if (today is tuesday and it is 8:00pm) {
it was not monday but it is tuesday and it is 8:00pm.
} else {
it must be sunday.
}
switch (day) {
case: Monday
it is monday
case: Tuesday
it is tuesday
}
A switch block pretty much demands the following:
- you have only one criterion to investigate, or several criterions that are non-dependent of each other
- you have several (i.e., more than 2 or 3) predefined branches
- you want to search for equality, for instance "day == Days.Monday"
If you want to check more than one codependent criterion at the same time, if you have just one branch (or maybe two small ones), or if you want to search for inequality (for instance "time > 8.00"), the if statement is the way to go.
Arguably, the first point in my list can be circumvented using various "nifty" techniques, but in my experience that only amounts to code that is convoluted and hard to read - and as T.J. stated, readability is a factor when writing maintainable code.
So the only real advantage I can think of for using the select block is for human readability..
Don't underestimate that. In programming, human readability should be the default No. 1 concern.