Purpose
This function evaluates expressions. You can call this function from the MapBasic window in MapInfo Pro.
Syntax
IIf( expression, truepart, falsepart )
expression The expression you want to evaluate.
truepart The value or evaluated expression returned when expression is True.
falsepart The value or evaluated expression returned when expression is False.
Description
The expression, truepart, and falsepart values are all required.
Mapbasic evaluates the expressions before passing them to the IIF function, so both truepart and falsepart get evaluated even though it returns only one of them.
Note About IIF() Versus If\Then
An If\Then argument evaluates its first argument and depending on that result continues to the second or third argument. MapBasic evaluates the expressions before passing them to the IIF function to evaluate all three arguments and get run-time errors (such as when dividing by zero). The following example shows this.
When the divisor is not zero (0), the IIf() function returns four (4).
Dim divisor, number as Integer
divisor = 3
number = 12
Print IIf(divisor <> 0, number \ divisor, 0)
If the divisor is zero (0), IIf() will cause a run-time error by dividing the number by zero (0) since MapBasic evaluates all three arguments before passing them to the function.
divisor = 0
Print IIf(divisor <> 0, number \ divisor, 0)
Example
Simple labeling of a world map where continent is Asia or not.
Set Map Window <mapID> Layer 1 Label With IIf(Continent="Asia", "Asia", "Other Continent")
Updating a Division column in the States table with a category depending on whether Population 1990 is greater than 6 million or not.
Update States Set Division = IIF(Pop_1990>6000000, "LargePop", "SmallPop") DropIndex Auto
Selecting from States where a population density column (Pop_Density) is less than 10 or not.
Select * from States where IIF(Pop_Density<10, Pop_Density, 0) into Selection
Creating a simple Individual theme where if State_Name = Alabama shade it green, otherwise shade it red.
shade window <mapID> 1 with Int(IIF(State_Name="Alabama", 1, 0)) values 0 Brush (2,16711680,16777215) Pen (1,2,0) ,1 Brush (2,65280,16777215) Pen (1,2,0) default Brush (1,0,16777215) Pen (1,2,0)
See Also: