Page 1 of 1 1
Topic Options
#180118 - 2007-09-04 01:50 AM If vs select
samd Offline
Fresh Scripter

Registered: 2002-12-03
Posts: 19
Can someone please give me some insite as to when an IF statement is better/worse than a SELECT statement.
I know there is no right or wrong but is there a proper place for either vs the other?

Top
#180119 - 2007-09-04 02:43 AM Re: If vs select [Re: samd]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Speaking as a non expert in the subject...

Select allows you to react upon more than a single evaluation of a "case".

If allows you to react upon only one evaluation of a "case"

If is great for a Y/N scenario

Select is better for a Y/N/M/I scenario

  • Yes
  • No
  • Maybe
  • I Don't Know
_________________________
Today is the tomorrow you worried about yesterday.

Top
#180120 - 2007-09-04 03:40 AM Re: If vs select [Re: Gargoyle]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Good answer. My rule of thumb is that if your checking a given variable for more than a two values (or settings) than use SELECT/CASE.
Top
#180121 - 2007-09-04 03:49 AM Re: If vs select [Re: Shawn]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I go by mutual exclusivity. If there is only one right answer, go with Select Case.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#180125 - 2007-09-04 04:57 AM Re: If vs select [Re: Les]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
And just to mix it up you can place an IF within Select or a Select within an IF

However I generally agree with Les on this if you're asking a few things but only one can be correct I'd use SELECT - you could do the same thing with IF but just not as clean (IMHO)

Top
#180128 - 2007-09-04 08:37 AM Re: If vs select [Re: NTDOC]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
I agree with Shawn, more then 2 possible anwers, go with Select, Else use If. hmmmmm...
 Code:
If $array
  Select
    Case 
  EndSelect
Else
  If EndIf
EndIf

Top
#180130 - 2007-09-04 09:28 AM Re: If vs select [Re: Arend_]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
When we say mutually exclusive, we don't necessarily mean that only one condition *is* true, we mean that we only want one condition to be actioned as true. Consider the following example:
 Code:
$iBorgThreat=udfEvaluateThreat("Borg")
  
Select
Case $iBorgThreat < 10
	$sAlertCondition="Green"
Case $iBorgThreat < 50
	$sAlertCondition="Yellow"
Case $iBorgThreat < 99
	$sAlertCondition="Red"
Case 1
	$sAlertCondition="Red"
	udfInitateSelfDestruct()
EndSelect


If $iBorgThreat is 40, then the first condition is not true - it is not less than 10 so it is not a "Green" thread. However, it *is* less than both 50 (yellow) and 99 (red), so both conditions are true.

The important thing to note about Select is that when a condition is found to be true none of the remaining conditions are evaluated. This means that the code works as you'd expect, and the Enterpise is set to condition yellow.

You can write all Select code as nested If/EndIf, however once you get more than three or four conditions the code becomes extremely difficult to read, and you are very likely to introduce an error if you make changes later.

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
0 registered and 1376 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

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

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