Difference between revisions of "VBScript logical operator"

From StealthBot Wiki Backup
Jump to: navigation, search
(Bitwise mistake)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 08:20, 1 December 2017

VBScript logical operators allow you to combine operations done by comparison operators to make "compound" If statements, among other things. Logical operators take in two (or one) Boolean values and return another Boolean value.

Logical conjunction operator: And

The logical And operator results in True only if both operands are True. In any other case, And will return False.

If A And B Then
    AddQ "Variable A and variable B equal True."
End If

For example, to combine embedded If blocks:

If IsOnline() Then
    If myUsername <> "Bob" Then
        Disconnect
    End If
End If

Becomes:

If IsOnline() And MyUsername <> "Bob" Then
    Disconnect
End If

The IsOnline function returns a Boolean and so does the <> operator.

Logical disjunction operator: Or

The logical Or operator results in True when either of the two operands are True. Only when both are False will Or return False.

If A Or B Then
    AddQ "Either variable A or variable B equals True."
End If

Logical negation operator: Not

The logical Not operator results in the opposite Boolean value of the single operand. Unlike other operators, Not only takes one operator to its right (it is a "unary" operator).

If Not A Then
    AddQ "Variable A equals False."
End If

For example, if we are not online, give an error:

If Not IsOnline() Then
    AddChat vbRed, "The bot cannot perform that function; it is offline."
End If

Bitwise mistake

A common mistake made is due to VBScript's tendency to "implicitly", or silently, correct variable type problems. If, for example, one takes the result of the built-in InStr() function (an integer result) and puts it in an If block:

If InStr("string", "s") Then
    AddQ "'s' is in 'string'."
End If

The integer type will be converted to a Boolean type silently, since If only takes a Boolean. If the function succeeds, it will return the index of the substring which is always greater than 0. Anything not equal to 0 will be converted to the Boolean True. If the function fails, it will return 0 which will be converted to False.

If Not InStr("string", "s") Then
    AddQ "'s' is not in 'string' (this may be the incorrect result)."
End If

If the user then tries to use the Not operator on the above, we will get the incorrect result because now we are invoking the bitwise Not operator which acts when the operand is an integer.

It is more correct to do the following instead:

If InStr("string", "s") > 0 Then
    AddQ "'s' is in 'string'."
End If
If InStr("string", "s") = 0 Then
    AddQ "'s' is not in 'string'."
End If

Logical exclusion operator: Xor

The logical Xor operator (exclusive or) results in True only when one of the two operands are True. When both are True or both are False, the result is False.

If A Xor B Then
    AddQ "Variable A equals True and variable B equals False, or variable A equals False and variable B equals True."
End If

This acts the same as:

If (A Or B) And Not (A And B) Then
    AddQ "Variable A equals True and variable B equals False, or variable A equals False and variable B equals True."
End If

Logical equivalence operator: Eqv

The logical Eqv operator ("equivalence") results in True when both operands are True or both operands are False. If one operand is True and the other is False, Eqv returns False.

If A Eqv B Then
    AddQ "Variable A is equivalent to variable B."
End If

This acts the same as:

If (A And B) Xor Not (A And B) Then
    AddQ "Variable A is equivalent to variable B."
End If

Logical implication operator: Imp

The logical Imp operator results in True in all cases when the second operand is True and when both operands are False. It results in False only when the first operand is True and the second is False.

If A Imp B Then
    AddQ "Variable A equals variable B or variable A equals False and variable B equals True."
End If

This acts the same as:

If (A Eqv B) Or (Not A And B) Then
    AddQ "Variable A equals variable B or variable A equals False and variable B equals True."
End If

Summary

First Operand Operator Second Operand Result
And:
True And True True
True And False False
False And True False
False And False False
Or:
True Or True True
True Or False True
False Or True True
False Or False False
Not:
Not True False
Not False True
Xor:
True Xor True False
True Xor False True
False Xor True True
False Xor False False
Eqv:
True Eqv True True
True Eqv False False
False Eqv True False
False Eqv False True
Imp:
True Imp True True
True Imp False False
False Imp True True
False Imp False True

See also