Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Else If - Vs - Select Case 1

Status
Not open for further replies.

TommyB44

Technical User
Jun 16, 2005
76
GB
Hi All,

I have some code and i'm using Else if to return the result of
a possible 190 options.

My question is, does anyone know which would run faster, the "else if" or the "select case" or are they the same ?.

Thanks for reading.
 
in case of SELECT CASE conditionals, a single expression (most often a variable), that is evaluated ONCE. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed.

on the other had in the case of IF ELSEIF conditionals...the expression is evaluated for every time until the condition is satisfied...

example: code taken from w3schools

Code:
select case payment
 case "Cash"
   msgbox "You are going to pay cash"
 case "Visa"
   msgbox "You are going to pay with visa"
 case "AmEx"
   msgbox "You are going to pay with American Express"
 case Else
   msgbox "Unknown method of payment"
end select

Code:
if payment="Cash" then
   msgbox "You are going to pay cash!"
 elseif payment="Visa" then
   msgbox "You are going to pay with visa."
 elseif payment="AmEx" then
   msgbox "You are going to pay with American Express."
 else
   msgbox "Unknown method of payment."
end If

as you can see...in the first code snippet the variable payment is evaluated only once and the block that satisfies the condition is executed...

on the other hand in the second case the variable is evaluated for everytime until the condition is satisfied...

hope that helps...

-DNG


 
I don't think the difference between if-elseif-else and select-case is as simple as that. Regardless of which statement you use, the value of the variable has to be compared to each target value. So the same number of comparisons has to be done regardless. I wouldn't be surprised if the resulting machine code was basically similar. It's true that with the select case the variable has to be evaluated only once, but if it's a variable, the term "evaluated" is meaningless - the variable is treated the same way regardless of which statement you use (i.e. if this memory location is equal to that memory location). The only time that "evaluation" is relevant is if, instead of a simple variable, an expression is used instead. Then, with the select-case that expression will only be evaluated once to reduce it to a simple value, whereas with the if-then-else it would be evaluated for each comparison. The obvious way around this, which all good programmers should be aware of, is to assign the results of the expression to a variable first, and use that variable in the if-elseif-else. Then you've got the same benefit of the select-case.

The main benefit of select-case is not efficiency, it coding convenience. It's really little more than "syntactic sugar".

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top