IIf
 
Conditional function that returns one of two values.

Syntax

IIf ( condition, expr_if_true, expr_if_false )

Parameters

condition
The condition to test.
A non-zero value evaluates as true, while a value of zero evaluates as false.
expr_if_true
An expression to evaluate and return if condition is true.
It must return a numeric value, which can be an integer, floating point number or a pointer.
expr_if_false
An expression to evaluate and return if condition is false.
It must be the same type of number as expr_if_true.

Description

IIf returns a different numeric value depending of the result of a conditional expression. Its typical use is in the middle of an expression; it avoids splitting it to put a conditional in the middle.

IIf only evaluates the expression that it needs to return. This saves time, and can also be useful to prevent evaluating expressions that might be invalid depending on the condition.

Example

Dim As Integer a, b, x, y, z
a = (x + y + IIf(b > 0, 4, 7)) \ z

is equivalent to:
Dim As Integer a, b, x, y, z, temp
If b > 0 Then temp = 4 Else temp = 7
a = (x + y + temp) \ z


Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __Iif.

Differences from QB

  • New to FreeBASIC

See also