I wish to know how this would impact my coding when handling large volumes of data, and how I would construct my logic arguments
Two questions:
1) What are the main differences between IF-ELSE and Select CASE? Does select case operate by evaluating all cases at the same time?
If I have a situation where, by nature of its construction, need to fulfill two or more cases at the same time, for e.g.
int = 5
Select case int
Case >0
Case 5
Case <0
Where I would need to "trigger" both Cases "1" and "2" for action, do I use CASE over IF ELSE?
2) Now for the other case, if I have a variable that will trigger more one case but I would like to restrict to only one case by priority, say I would only like Case "1" for action and exclude the rest, am I right to say that IF-ELSE would triumph in this regard?
EDIT - I realized that my question was phrased poorly. I did some edits to the code
If both statements are applicable, then SELECT CASE is more clear because it applies in only a subset of the cases for which IF/ELSEIF applies.
Speed: A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too.
A Select Case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each select case.
A switch statement works much faster than an equivalent if-else ladder. It's because the compiler generates a jump table for a switch during compilation. As a result, during execution, instead of checking which case is satisfied, it only decides which case has to be executed.
Some clarification on Select Case
before actually answering your question:
Case
switch in VBA, e.g.: Case Is>40, 20, 10, 2 To 3
, for more information see documentation on MSDN
Case
, it'll exit after finding a match (e.g. in the example execute Case >0
then exit without even evaluating Case 5
)if you want to execute "both" for a value you can nest if
within case:
Case Is > 0
<Common code>
If i = 5 Then
<code only for case 5>
End If
Select Case
if
If
Select Case
e.g.:
if
and select
, but if
is easier to read)Select Case
you can also use elseif to prepare multiple comparisons, it'll also stops at first fullfilled criteria, but here your expression will be evaluated every time until finding the match and of course you can use more complex criteria.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With