# If 運算子 (Visual Basic)If Operator (Visual Basic)

## 語法Syntax

If( [argument1,] argument2, argument3 )


## 如果使用三個引數來呼叫運算子If Operator Called with Three Arguments

If稱為藉由使用三個引數，第一個引數必須評估為值，可以轉換成BooleanWhen If is called by using three arguments, the first argument must evaluate to a value that can be cast as a Boolean. Boolean值將決定哪些其他兩個引數會評估並傳回。That Boolean value will determine which of the other two arguments is evaluated and returned. 下列清單時，才適用If使用三個引數呼叫運算子。The following list applies only when the If operator is called by using three arguments.

## 組件Parts

argument1 必要項。Required. Boolean.Boolean. 判斷其中一個要評估及傳回的其他引數。Determines which of the other arguments to evaluate and return.
argument2 必要項。Required. Object.Object. 評估並傳回 ifargument1評估為TrueEvaluated and returned if argument1 evaluates to True.
argument3 必要項。Required. Object.Object. 評估並傳回 ifargument1評估為False或者argument1Nullable Boolean變數評估為NothingEvaluated and returned if argument1 evaluates to False or if argument1 is a NullableBoolean variable that evaluates to Nothing.

If運算子，會使用三個引數呼叫的運作方式類似IIf函式不同之處在於它會使用最少運算評估。An If operator that is called with three arguments works like an IIf function except that it uses short-circuit evaluation. IIf函式一律會評估其引數，這三個，而If有三個引數的運算子會評估只有兩個。An IIf function always evaluates all three of its arguments, whereas an If operator that has three arguments evaluates only two of them. 第一個If引數會評估和結果轉換成Boolean的值，TrueFalseThe first If argument is evaluated and the result is cast as a Boolean value, True or False. 如果值為Trueargument2會評估並傳回其值，但argument3不會評估。If the value is True, argument2 is evaluated and its value is returned, but argument3 is not evaluated. 如果的值Boolean運算式Falseargument3會評估並傳回其值，但argument2不會評估。If the value of the Boolean expression is False, argument3 is evaluated and its value is returned, but argument2 is not evaluated. 下列範例說明使用If三個引數使用時：The following examples illustrate the use of If when three arguments are used:

' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))


number = 12

' When the divisor is not 0, both If and IIf return 4.
Dim divisor = 3
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))

' When the divisor is 0, IIf causes a run-time error, but If does not.
divisor = 0
Console.WriteLine(If(divisor <> 0, number \ divisor, 0))
' Console.WriteLine(IIf(divisor <> 0, number \ divisor, 0))


## 組件Parts

argument2 必要項。Required. Object.Object. 必須是參考或可為 null 的型別。Must be a reference or nullable type. 評估並傳回其評估結果為任何項目以外時NothingEvaluated and returned when it evaluates to anything other than Nothing.
argument3 必要項。Required. Object.Object. 評估並傳回 ifargument2評估為NothingEvaluated and returned if argument2 evaluates to Nothing.

Boolean省略引數，第一個引數必須是參考或可為 null 的型別。When the Boolean argument is omitted, the first argument must be a reference or nullable type. 如果第一個引數評估為Nothing，會傳回第二個引數的值。If the first argument evaluates to Nothing, the value of the second argument is returned. 在其他情況下，會傳回第一個引數的值。In all other cases, the value of the first argument is returned. 下列範例說明這項評估的運作方式。The following example illustrates how this evaluation works.

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))