# Conditional and Logical Operators in Visual Basic .NET

## Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

#### This lesson describes the Conditional and Logical operators.

For building **complex expressions**, VB.NET offers an expected set of **Logical and Bitwise Operators** as shown below:

**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.)*

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)*

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.)*

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)*

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.)*

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.)*

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

Share This

Subscribe

More Tutorials

Popular