Sign in to follow this  
Followers 0
Arrrghmatey

Faster: if / elseif or Case?

6 posts in this topic

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?

Share this post


Link to post
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.

Share this post


Link to post
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

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

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

<{POST_SNAPBACK}>

Yeah, the second case isn't executed.

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

Share this post


Link to post
Share on other sites

And to add to that, if you DO want it to check each If statement, you'd actually have to have a seperate If..EndIf statement for each check.

Something like the following:

$i = 3

If $i > 1 Then
    MsgBox(0,"case","first")
EndIf
If $i > 2 Then
    MsgBox(0,"case","second")
EndIf

Share this post


Link to post
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...

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
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
Sign in to follow this  
Followers 0