#121027 - 2004-06-10 03:37 PM
SELECT-ENDSELECT vs. IF-ENDIF
|
Moonpie
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
|
|
|
|
#121029 - 2004-06-10 05:06 PM
Re: SELECT-ENDSELECT vs. IF-ENDIF
|
Moonpie
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
|
|
|
|
#121031 - 2004-06-10 06:04 PM
Re: SELECT-ENDSELECT vs. IF-ENDIF
|
Richard H.
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Henriques)
and 2516 anonymous users online.
|
|
|