Powered by Blogger.

Conditional and Logical Operators

Not Is performing logical negation on a Boolean expression, or bitwise negation on a numeric expression. (For a Boolean negation, the data type of the result is Boolean. For a bitwise negation, the result data type is the same as that of expression but if expression is Decimal, the result is Long.)


1
2
Dim IsUsernameValid As Boolean
IsUsernameValid = Not (Username.Text = "admin")


And Is performing a logical conjunction on two Boolean expressions, or a bitwise conjunction on two numeric expressions. (In a Boolean comparison, the And operator always evaluates both expressions)

1
2
Dim IsUserValid As Boolean
IsUserValid = (Username.Text = "admin") And (Password.Text = "mypwd")


AndAlso Is performing short-circuiting logical conjunction on two expressions (In a Boolean comparison, AndAlso performs short-circuiting, which means that if expression1 is False, then expression2 is not evaluated.)

1
2
Dim IsUserValid As Boolean
IsUserValid = (Username.Text = "admin") AndAlso (Password.Text = "mypwd")


Or Is performing a logical disjunction on two Boolean expressions, or a bitwise disjunction on two numeric expression (In a Boolean comparison, the Or operator always evaluates both expressions)

1
2
Dim IsPasswordValid As Boolean
IsPasswordValid = (Password.Text = "mypass") Or (Password.Text = "mypwd")


OrElse Is performing short-circuiting inclusive logical disjunction on two expressions (In a Boolean comparison, OrElse performs short-circuiting which means that if expression1 is True, then expression2 is not evaluated.)

1
2
Dim IsPasswordValid As Boolean
IsPasswordValid = (Password.Text = "mypwd") OrElse (Password.Text = "mypass")


Xor Is performing a logical exclusion on two Boolean expressions, or a bitwise exclusion on two numeric expressions. (In a Boolean comparison, the Xor operator always evaluates both expressions as there is no short-circuiting counterpart to Xor, because the result always depends on both operands.)

1
2
Dim IsUsernameValid As Boolean
IsUsernameValid = (Username.Text = "admin") Xor (Username.Text = "user")