Select Case Vs If - ElseIf 
Author Message
 Select Case Vs If - ElseIf

Hi everyone,

This might look like a silly question but I was wondering if anyone had a
clue on whether to use Select case or If-Elseif's. Which one is faster? I
tend to use select case because is more "beautiful"...

Thanx.



Sun, 30 Sep 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf
The whole purpose of the Select Case structure is to make your code easier
to read than with a If ... Then ... Else statement.  Keep in mind though
that you can only replace an If...Then...Else structure with a Select Case
structure if the If statement and each ElseIf statement evaluate the same
expression ... since a select case only evaluates an expression once at the
top ... whereas a If ... Then ... Else can evaluate at each level.

**** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ****



Sun, 30 Sep 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf
I would also add that because of the extra logic that If ... Else ... Then
handles ... it's very plausible to assume that it could be slower.

**** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ****



Sun, 30 Sep 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf

Quote:

>This might look like a silly question but I was wondering if anyone had a
>clue on whether to use Select case or If-Elseif's. Which one is faster?

Since I expect both to compile pretty much into the same code (test of
expression, and conditional jump), I don't expect any significant speed
difference. Anyway, these are straightforward code, therefore they
should be fast compared to the rest of the code. Therefore, the question
should be moot.

Quote:
>I tend to use select case because is more "beautiful"...

That's what it's for. It's a high-level kanguage, after all.

        Bart.



Sun, 30 Sep 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf
My previous responses are base on Microsoft documentation.  Below is an
exerpt:

Visual Basic provides the Select Case structure as an alternative to
If...Then...Else for selectively executing one block of statements from
among multiple blocks of statements. A Select Case statement provides
capability similar to the If...Then...Else statement, but it makes code more
readable when there are several choices.

**** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ****



Tue, 02 Oct 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf

Quote:

> Hi everyone,

> This might look like a silly question but I was wondering if anyone had a
> clue on whether to use Select case or If-Elseif's. Which one is faster? I
> tend to use select case because is more "beautiful"...

> Thanx.

Speedwise I don't think there is any difference. But in some situations
it's much more elegant like this:

    Select Case strA
        Case "Q", "V", "X"
            MsgBox "1"
        Case "M", "A" To "L"
            MsgBox "2"
    End Select

Writing this with If/Else would be much uglier.

What I know for sure is that IIf is slower !!!



Sat, 13 Oct 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf
Hi all;

Quote:


>> Hi everyone,

>> This might look like a silly question but I was wondering if anyone had a
>> clue on whether to use Select case or If-Elseif's. Which one is faster? I
>> tend to use select case because is more "beautiful"...

>> Thanx.

>Speedwise I don't think there is any difference. But in some situations
>it's much more elegant like this:

>    Select Case strA
>        Case "Q", "V", "X"
>            MsgBox "1"
>        Case "M", "A" To "L"
>            MsgBox "2"
>    End Select

>Writing this with If/Else would be much uglier.

>What I know for sure is that IIf is slower !!!

And I like it cause I can do this:

Public Function BooleanProcedure(Param1 As Variant) As Boolean
'
    On Error GoTo ErrorHandler
ErrorHandler:
    Select Case True        ' Sanity checks
        Case Err
            Debug.Print ERRMSG; Err.Description

        Case Not BooleanProcedure2(Param1)

        Case Not BooleanProcedure3

        Case Else
            ' Passed Sanity checks ...

            MsgBox "Place code here."

            BooleanProcedure = True
    End Select
End Function

Hope this helps;
Rick



Sat, 13 Oct 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf

Imagine you  have :

if then ------ endif
if then ------ endif
if then ------ endif
if then ------ endif
x 1000

The processor will pass trough all the 10000 IF-ENDIF structures

now with this

Case ------ End case
Case ------ End case
Case ------ End case
Case ------ End case
x1000

If the condition is verified at the first Case then the processor wont pass
through the other 'case
structures'. But if the condition  is verified at the last Case, then If
structure and Case structures are equivalent.

What I can say is that the IF structure is more flexible (multiple
variables) and the Case is more efficient and readable (but with a case you
can only check a condition of one variable at a time... - select case (xxx)
...)

fabien SPAGNOLO
Universit Lyon1


Quote:

>> Hi everyone,

>> This might look like a silly question but I was wondering if anyone had a
>> clue on whether to use Select case or If-Elseif's. Which one is faster? I
>> tend to use select case because is more "beautiful"...

>> Thanx.

>Speedwise I don't think there is any difference. But in some situations
>it's much more elegant like this:

>    Select Case strA
>        Case "Q", "V", "X"
>            MsgBox "1"
>        Case "M", "A" To "L"
>            MsgBox "2"
>    End Select

>Writing this with If/Else would be much uglier.

>What I know for sure is that IIf is slower !!!



Sun, 14 Oct 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf

Quote:

>Imagine you  have :

>if then ------ endif
>if then ------ endif
>if then ------ endif
>if then ------ endif
>x 1000

>The processor will pass trough all the 10000 IF-ENDIF structures

You never heard of "elseif"?

        Bart.



Sun, 14 Oct 2001 03:00:00 GMT  
 Select Case Vs If - ElseIf

Quote:
>You never heard of "elseif"?

I'm glad you mentioned this...  I'm not sure about VB, but *ElseIf* is the
typical way a Case statement is "coded" by the compiler.  When it translates it
to assembly or machine language (depending on the tool), it usually creates one
huge If...ElseIf...End statement from the Case statement.

For example, I would assume VB would convert this:

Select Case key
Case 1
  ...
Case 2
  ...
Case 3
  ...
Case Else
  ...
End Select

To roughly this (minus optimizations):

If key = 1 then
  ...
ElseIf key = 2 then
  ...
ElseIf key = 3 then
  ...
Else
  ...
End If

As I understand, this is basically what most C/C++ engines and the Delphi
engine does.  I would assume VB would be the same as I think it's the most
efficient way to translate a Case that I can think of.  Granted though, this
may be why I'm not a dev tools developer.  ;)

-Curtis Spendlove
-Solstice Software



Sun, 14 Oct 2001 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. If..Elseif vs. Select case

2. VBScript Select Case vs VB Select Case

3. Select case/if then elseif

4. Academic Question - Select Case vs. If ElseIf - which is better

5. Case Select vs If then Else

6. "select...case", multiples cases not possible ?

7. Select Case and Case 0 To 9

8. Select case ... OR If...Elseif... ??

9. select * vs select fields

10. IF vs Case ?

11. If vs. Case

12. IF...THEN Vs. CASE Statements

 

 
Powered by phpBB® Forum Software