Operator

# And

## Description

Used to perform a logical comparison of two boolean expressions And a bitwise And comparison of two integer expressions.

## Usage

```result = expression1 And expression2
```

Part

Description

result

A Boolean if expression1 and expression2 are booleans; an integer if expression1 and expression2 are integers.

expression1

Any valid boolean or integer expression. The data types of expression1 and expression2 must match.

expression2

Any valid boolean or integer expression. The data types of expression1 and expression2 must match.

## Notes

The And operator is overloaded. You can pass either two booleans or two integers. If you pass two booleans, And performs a logical And operation And returns a boolean. If you pass two integers, it performs a bitwise And operation And returns an integer. In this case, it is equivalent to calling the BitAnd method of the Bitwise class.

In the first instance, if both boolean expressions evaluate to True, then result is True. If either expression evaluates to False then result is False.

The truth table for logical operators is shown below.

Expression1

Expression2

And

Or

Xor

True

True

True

True

False

True

False

False

True

True

False

True

False

True

True

False

False

False

False

False

If you pass two integers, And returns an integer that is the result of comparing each bit of the two integers And assigning 1 to the bit position in the integer returned if both bits in the same position in the integers passed are 1. Otherwise, 0 is assigned to the bit position.

The following table gives the results for bitwise operators.

Integer1

Integer2

And

Or

Xor

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

The Operator And function can be used to define the And operator for classes.

### Use within if statements

When used within an If..Then statement, once the first expression results to False, none of the subsequent expressions are evaluated. In this example, the IsValid method is not called because the first expression is False. This is referred to as short-circuit evaluation:

```Var b As Boolean = False
If b And IsValid("value") Then ' IsValid does not get called
...
End If
```

## Sample code

This example uses the And operator to perform logical comparisons.

```Var a As Boolean
Var b As Boolean
Var c As Boolean ' defaults to False
Var d As Boolean ' defaults to False

a = True
b = True
d = a And b ' Returns True
d = a And c ' Returns False
```

Using And with Integer values:

```Var i1 As Integer = 4
Var i2 As Integer = 5

Var result As Integer
result = i1 And i2 ' result = 4
```

## Compatibility

All project types on all supported operating systems.