Using the In-line If (IIF) Function

The traditional way to configure one or more functions to run conditionally is by using If and Else statements. A relatively simple alternative is the In-line If (IIF) function. The IIF function provides much of the functionality that is available with If and Else statements, but it does not require you to use multiple lines of Visual Basic. In fact, the IIF function is the only way to configure a single-line expression to run conditionally. It is also easy enough to use that many users use it when possible in multi-line scripts, even though they could use separate If and Else statements.

IIF Syntax

The IIF function syntax is as follows.

IIF(<ConditionalExpression>,<ExpressionIfTrue>,<ExpressionIfFalse>)

Although the IIF function is available in a number of computer languages, it is not native to Visual Basic. The manufacturers of BarTender created the version that is provided in the Visual Basic Script Editor for your use.

The IIF function contains the following expressions:

Only one of the two expressions ("ExpressionIfTrue" or "ExpressionIfFalse") is run each time the IIF function runs. Either of these expressions can be any valid single line expression, including additional ("nested") IIF functions.

Examples

The IIF function first examines the conditional expression (the first term in the function) to determine whether it is True or False. Examples of a conditional expression include the following:

Related Topics