IIf Function

依據所演算表示式的邏輯值,傳回兩個可能的函式結果之一。

Syntax:


IIf (Expression, ExpressionTrue, ExpressionFalse)

Parameters:

Expression:要演算的任意表示式。如果表示式的演算值為 True (真),函式將傳回 ExpressionTrue 的結果,否則將傳回 ExpressionFalse 的結果。

ExpressionTrue、ExpressionFalse:任意表示式,其中的一個將作為函式結果傳回,這取決於邏輯求值的結果。

note

IIf evaluates both ExpressionTrue and ExpressionFalse even if it returns only one of them. If one of the expressions results in error, the function returns the error. For example, do not use IIF to bypass a possible division by zero result.


錯誤代碼:

5 無效的程序呼叫

Example:


REM Returns the maximum of 3 values
Function Max (A As Double, B As Double, C, As Double) As Double
    Max = IIf( A >= B, A, B)
    Max = IIf( C >= Max, C, Max)
End Function
REM Bad usage of function IIf
Function Inverse(A As Double) As Double
    Inverse = IIf( A = 0, 0, 1/A )
End Function

If or Select Case statements

Switch function

Please support us!