Jump to content

Faster: if / elseif or Case?


Recommended Posts

Right now I have 7 elseif starements like this:

If X Then
     ...
ElseIf Y Then
     ...
ElseIf Z Then
.
.
.
Else
     ...

Would it be faster if I used a Select Case instead? When using elseif, does it check each elseif, or once a condition is satisfied it doesn't check the others?

Link to comment
Share on other sites

From the helpfile:

If...ElseIf...Else...EndIf : If the expression is true, the first statement block is executed. If not, the first true ElseIf block is executed. Otherwise, the "Else" block is executed.

Select...Case...EndSelect : If the expression is true the following statements up to the next Case or EndSelect statement are executed. If more than one of the Case statements are true, only the first one is executed.

Link to comment
Share on other sites

From the helpfile:

If...ElseIf...Else...EndIf : If the expression is true, the first statement block is executed. If not, the first true ElseIf block is executed. Otherwise, the "Else" block is executed.

Select...Case...EndSelect : If the expression is true the following statements up to the next Case or EndSelect statement are executed. If more than one of the Case statements are true, only the first one is executed.

<{POST_SNAPBACK}>

run this to see it for yourself

$i = 3

select 
case $i > 1
    MsgBox(0,"case","first")
case $i > 2
    MsgBox(0,"case","second")
EndSelect
Link to comment
Share on other sites

Yeah, the second case isn't executed.

I pointed the difference of both here, according to the documentation.

<{POST_SNAPBACK}>

yes, your explanation was right on, and probably didn't need any further elaboration, and i didn't mean to imply otherwise. i just like to give a practical example when possible... old habits die hard...
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...