473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Select Case vs. ElseIf

Is there any speed/resource advantage/disadvantage in using

Select Case x
Case 1
Case 2
etc. many more cases...
End Select

VS.

If x Then
ElseIf x+1 Then
ElseIf x+2 Then
etc. many more ElseIfs...
End If

Nov 13 '05 #1
7 9294
Lauren Quantrell wrote:
Is there any speed/resource advantage/disadvantage in using

Select Case x
Case 1
Case 2
etc. many more cases...
End Select

VS.

If x Then
ElseIf x+1 Then
ElseIf x+2 Then
etc. many more ElseIfs...
End If


Now adays I suspect not. Its more a question of readability so I vote for a properly
tabbed Select Case...

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #2
I tend to have a lot of

SELECT CASE x
CASE x1

SELECT CASE y
CASE y1
CASE y2
END SELECT

CASE x2
etc.
END SELECT

Nov 13 '05 #3
Lauren Quantrell wrote:
I tend to have a lot of

SELECT CASE x
CASE x1

SELECT CASE y
CASE y1
CASE y2
END SELECT

CASE x2
etc.
END SELECT


If you have Case statement within your Case statements then it would be more readable to
use a function/sub (IMO) to prevent the logic from getting too hard to follow:

SELECT CASE x
CASE x1
getSomeValueOrDoSomeThing
CASE x2
etc.
END SELECT
....
Private Sub getSomeValueOrDoSomeThing()
....
End Sub

btw..indentation is a personal preference but be consistent in your implementation.
--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #4
There's one case in which (AFAIK) it makes a difference: if you're selecting
based on something calculated or read from an object, rather than a
variable. With IF you'd need to calculate it each time, or put it into a
variable, with Select you don't. In that case If would slow your process
down (more or less depending on the complexity of your calculation) or
create more variables.

I have a lot of these:

Select Case Len(TestString)

Case Is = 1

Case Is = 2

Case Is => 3

End Select

It's a lot cleaner with Select.

"Lauren Quantrell" <la*************@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Is there any speed/resource advantage/disadvantage in using

Select Case x
Case 1
Case 2
etc. many more cases...
End Select

VS.

If x Then
ElseIf x+1 Then
ElseIf x+2 Then
etc. many more ElseIfs...
End If

Nov 13 '05 #5
On Wed, 19 Oct 2005 04:27:06 GMT, John Mishefske <jm**********@SPAMyahoo.com>
wrote:
Lauren Quantrell wrote:
I tend to have a lot of

SELECT CASE x
CASE x1

SELECT CASE y
CASE y1
CASE y2
END SELECT

CASE x2
etc.
END SELECT


If you have Case statement within your Case statements then it would be more readable to
use a function/sub (IMO) to prevent the logic from getting too hard to follow:

SELECT CASE x
CASE x1
getSomeValueOrDoSomeThing
CASE x2
etc.
END SELECT
...
Private Sub getSomeValueOrDoSomeThing()
....
End Sub

btw..indentation is a personal preference but be consistent in your implementation.


Just to add another opinion on the Select Case indentation style...

I use one of 2 styles for a case statement. If the statements are short
enough to fit on the same lines with the cases using a colon separator, then I
indent the Case lines. otherwise, I don't indent the case lines. Having 3
levels of indentation for a single branch structure, just seems to be hard to
read for no good reason, to me.

SELECT CASE x
CASE x1: getSomeValueOrDoSomeThing
CASE x2: etc.
END SELECT

Or

SELECT CASE x
CASE x1
getSomeValueOrDoSomeThing
CASE x2
etc.
END SELECT

In either case, the lines with the code that perform the actions are indented,
and lines that are -only- part of the branch logic are not. This is coherent
with the standard indentation for If/Then/Else.
Nov 13 '05 #6
On 18 Oct 2005 21:06:02 -0700, "Lauren Quantrell"
<la*************@hotmail.com> wrote:
I tend to have a lot of

SELECT CASE x
CASE x1

SELECT CASE y
CASE y1
CASE y2
END SELECT

CASE x2
etc.
END SELECT


There's usually a way to unstack these, and greatly improve reuse and
mainainability in the process. Try to set it up so that the X cases write
parial results to variables, then a separate block for Y finishes the job.

For instance, If you start with something like this...

Select case X
Case -1
Select Case Y
Case -1: Cell = "Top Left"
Case 0: Cell = "Left"
Case 1: Cell = "Bottom Left"
End Select
Case 0
Select Case Y
Case -1: Cell = "Top"
Case 0: Cell = "Center"
Case 1: Cell = "Bottom"
End Select
Case 1
Select Case Y
Case -1: Cell = "Top"
Case 0: Cell = "Center"
Case 1: Cell = "Bottom"
End Select
End Select

.... you can rewrite it like this...

Select case X
Case -1: XText = "Left"
Case 1: XText = "Right"
End Select

Select case Y
Case -1: YText = "Top"
Case 1: YText = "Bottom"
End Select

Cell = XText & " " & YText
If Cell = " " Then Cell = "Center"
Nov 13 '05 #7
"Lauren Quantrell" <la*************@hotmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:
Is there any speed/resource advantage/disadvantage in using

Select Case x
Case 1
Case 2
etc. many more cases...
End Select

VS.

If x Then
ElseIf x+1 Then
ElseIf x+2 Then
etc. many more ElseIfs...
End If


Others have given you good answers, and to your specific question
about the nesting of multiple levels of CASE SELECT.

There's another issue:

Sometimes a CASE SELECT is *not* equvalent.

If/Then/Else is processed in sequence, and each control line (the
If/Then, the ElseIf/Then and the Else lines) is processed
independently. That means you have 2 or more tests that you can run,
and they don't have have to be run on the same expression.

CASE SELECT operates on a single expression, usually testing a
single value.

CASE SELECT can do more than testing equality, though:

CASE SELECT lngVariable
CASE <0
[do whatever]
CASE 0
[do whatever]
CASE >0
[do whatever]
END SELECT

I see no reason why that is superior to (I'll explain the odd logic
later):

If lngVariable < 0 THen
[do whatever]
Else lngVariable = 0 Then
[do whatever]
Else
[do whatever]
End If

Now, I don't remember the details on this, but I believe CASE SELECT
tests in order, down the list, so you're going to test 3 values for
numbers >0, 2 for 0 and 1 for <0. With the If/Then there's only a
maximum of 2 comparisons, as the last two possibilities are handled
by a match or a non-match.

You could do the same with:

CASE SELECT lngVariable
CASE <0
[do whatever]
CASE 0
[do whatever]
CASE ELSE
[do whatever]
END SELECT

Of course, the natural order for a CASE SELECT is ordered for
numbers or alphabitical for strings (makes it easier to read),
though you may choose to use a different order if you have a
non-random distribution of values.

In any event, testing for equality with 0 is probably going to
happen less often than <> 0, so I'd leave that as the last ELSE in
either structure. So, that would give you:

CASE SELECT lngVariable
CASE <0
[do whatever]
CASE >0
[do whatever]
CASE ELSE
[do whatever]
END SELECT

And:

If lngVariable < 0 THen
[do whatever]
Else lngVariable > 0 Then
[do whatever]
Else
[do whatever]
End If

Now, that's one set of issues.

THe other is that you may be doing a Boolean test on more than one
expression. In that case, CASE ELSE becomes complicated. Your
nesting example is exactly that. It could have been expressed as a
Boolean expression of ((x = 1) and (y = 1)), though your particular
example doesn't need to be expressed that way.

The point is that you may have non-symmetrical tests. You may do
some completely different branch of code for the NOT case of such a
set of tests. In that case, If/Then/Else is probably better, or a
mix of it with CASE/SELECT at the lower level.

So, balanced trees can use either structure, with CASE SELECT being
better, while unbalanced trees often are better suited to
If/Then/Else.

Hope that nakes sense!

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
9
by: Kevin | last post by:
Hi, I am getting a syntax error Microsoft VBScript compilation error '800a03ea' Syntax error On the code below. The error references the "End Select" line Can anyone help me with what I am...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
3
by: mark.irwin | last post by:
Hello all, Have an issue where a redirect pushes data to a page with a select case which then redirects to another page. Problem is the redirect isnt working in 1 case. Code below: strURL =...
6
by: mabond | last post by:
Hi Is it possible to compare a vaule for a select case statement using a wildcard. e.g. somthing like Select case myValue case like "*ing" end select
5
by: PJ6 | last post by:
Select Case o.GetType Case = GetType(SomeRandomTypeName) '... End Select Why doesn't this work? Can I make it work or am I stuck with ElseIf for a long list? Paul
21
beacon
by: beacon | last post by:
Hello to everybody, I have a section on a form that has 10 questions, numbered 1-10, with 3 option buttons per question. Each of the option buttons have the same response (Yes, No, Don't know),...
4
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.