Page 1 of 1 1
Topic Options
#121027 - 2004-06-10 03:37 PM SELECT-ENDSELECT vs. IF-ENDIF
Moonpie Offline
Lurker

Registered: 2004-06-10
Posts: 2
Loc: North Carolina
Hi all...

Anyone know exactly how SELECT and IF statements are processed and which is more efficient?

I have some vague recollection that with one, the entire statement is processed, even if the 1st condition is true and with the other, the statement is exited as soon as it finds a "true" condition.

Thanks...

Top
#121028 - 2004-06-10 03:59 PM Re: SELECT-ENDSELECT vs. IF-ENDIF
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
There is no issue of efficiency.

You can construct a "SELECT/ENDSELECT" structure with IF/ELSE/ENDIF statements, but unless it was very small it would become pretty hard to read, understand and update.

Use a SELECT structure when you only ever want one of the conditions to be true, i.e. the CASEs are all mutually exclusive.

Use IF structures when more than one of the conditions may be true.

Top
#121029 - 2004-06-10 05:06 PM Re: SELECT-ENDSELECT vs. IF-ENDIF
Moonpie Offline
Lurker

Registered: 2004-06-10
Posts: 2
Loc: North Carolina
Thanks for the quick response, Richard - your concise explanation of how to choose between the methods is very helpful!

Top
#121030 - 2004-06-10 05:18 PM Re: SELECT-ENDSELECT vs. IF-ENDIF
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quote:

There is no issue of efficiency



Not quite true. Since SELECT CASE enumerates to the first true CASE, I believe it would not bother to enumerate any subsequent CASEs, hence efficiency. IF statements would continue enumerating.

The real factor to consider though is mutual exclusivity, which you did cover.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#121031 - 2004-06-10 06:04 PM Re: SELECT-ENDSELECT vs. IF-ENDIF
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Quote:

There is no issue of efficiency



Not quite true. Since SELECT CASE enumerates to the first true CASE, I believe it would not bother to enumerate any subsequent CASEs, hence efficiency. IF statements would continue enumerating.




No, if you write a "select" type construct using IFs it is just as efficient, as it works in the same way.

As an example, take a simple SELECT construct:
Code:
$Value=3
Select
Case $Value > 2
"Value is greater than 2" ?
Case $Value > 1
"Value is greater than 1" ?
EndSelect



Constructed with IFs this becomes:
Code:
$Value=3
If $Value > 2
"Value is greater than 2" ?
Else If $Value > 1
"Value is greater than 1" ?
EndIf
EndIf



They both do the same thing. Using IFs however as I mentioned is hard to read and difficult to maintain.

If you coded this badly with IFs like so:
Code:
$Value=3
If $Value > 2
"Value is greater than 2" ?
EndIf
If $Value > 1
"Value is greater than 1" ?
EndIf



Not only is this less efficient as both conditionals are evaluated, but it is actually completely different logic than the previous two example as both conditionals will evaluate as true.

Thinking about it a little more, there may be a small overhead in maintaining the IF stack - or there may not. It depends on the internal KiXtart mechanisms.

Top
#121032 - 2004-06-10 06:10 PM Re: SELECT-ENDSELECT vs. IF-ENDIF
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Not fair... nested IFs were not mentioned. Of course one can "emulate" SELECT CASE constructs by nesting, but I did not infer nesting as being in the original question.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#121033 - 2004-06-10 06:25 PM Re: SELECT-ENDSELECT vs. IF-ENDIF
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
I believe there was an article by Dan Appleman of Desaware about a decade ago where he demonstrated that the IF structure did in fact take longer to process and used more memory in Visual Basic (I think v3 at the time). Not sure that similar numbers would exist in KiXtart or not, someone had run some minor testing with loops that showed one was just barely faster then the other but only after thousands of iterations.

However, I think that aside from being a techno geek that is looking for the millisecond difference or whatever... Richard has helped MoonPie to understand the why of using which method. I doubt anyone is really writing Logon or Admin scripts that are really going to make all that much of a difference in speed regardless of which method is used.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.061 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org