473,686 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9330
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
getSomeValueOrD oSomeThing
CASE x2
etc.
END SELECT
....
Private Sub getSomeValueOrD oSomeThing()
....
End Sub

btw..indentatio n 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.goo glegroups.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**********@S PAMyahoo.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
getSomeValueOrD oSomeThing
CASE x2
etc.
END SELECT
...
Private Sub getSomeValueOrD oSomeThing()
....
End Sub

btw..indentati on 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: getSomeValueOrD oSomeThing
CASE x2: etc.
END SELECT

Or

SELECT CASE x
CASE x1
getSomeValueOrD oSomeThing
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.goo glegroups.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
14631
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 allow entry to one or the other or Both at the same time. Now I tried to use an If ElseIf but it got too hard to track, so now I am using a SELECT CASE Statement.
9
3630
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 doing wrong? Thanks
10
2162
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
3392
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 = "" if i = 1 then strURL = "redirect.aspx?page=APIQ&parcel=" & strParcel &
6
28728
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
10115
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
17435
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), but each answer is given a different point value for each question. For instance, question 1 might ask "Do you like candy?" and if you answer Yes you will get +1, No will earn you -1, and Don't know will net you zero points. The next question might...
4
10668
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, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from products_description pd,...
3
2209
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 that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another menu appears with options based on that city. I can't seem to get the input from these fields in the...
0
8516
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8768
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7599
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.