Using the In-line If (IIF) Function

The traditional way to add conditional execution to one or more functions is with If and Else statements. A relatively simple alternative is the In-line If function (abbreviated IIF). The IIF function provides much of the functionality available with If and Else statements, but it doesn't require the use of multiple lines of Visual Basic. In fact, it is the only way to add conditional execution to a single-line expression. It is also easy enough to work with that many users will use it when possible in multi-line scripts, even though they could have used 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 provided in the Visual Basic Script Editor for your use.

The IIF function contains three expressions:

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

Examples of Using the IIF Function

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

Related Topics