Operator

# And

<div class="rst-class">

forsearch

</div>

Operator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to perform a logical comparison of two `boolean</api/data_types/boolean>` expressions <span class="title-ref">And</span> a bitwise <span class="title-ref">And</span> comparison of two `integer</api/data_types/integer>` expressions.

## Usage

``` xojo
result = expression1 And expression2
```

| Part        | Description                                                                                                                                                                  |
|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| result      | A `Boolean</api/data_types/boolean>` if *expression1* and *expression2* are booleans; an `integer</api/data_types/integer>` if *expression1* and *expression2* are integers. |
| expression1 | Any valid `boolean</api/data_types/boolean>` or `integer</api/data_types/integer>` expression. The data types of *expression1* and *expression2* must match.                 |
| expression2 | Any valid `boolean</api/data_types/boolean>` or `integer</api/data_types/integer>` expression. The data types of *expression1* and *expression2* must match.                 |

## Notes

The <span class="title-ref">And</span> operator is overloaded. You can pass either two `booleans</api/data_types/boolean>` or two `integers</api/data_types/integer>`. If you pass two booleans, <span class="title-ref">And</span> performs a logical <span class="title-ref">And</span> operation <span class="title-ref">And</span> returns a `boolean</api/data_types/boolean>`. If you pass two integers, it performs a bitwise <span class="title-ref">And</span> operation <span class="title-ref">And</span> returns an `integer</api/data_types/integer>`. In this case, it is equivalent to calling the BitAnd method of the `Bitwise</api/language/bitwise>` class.

In the first instance, if both `boolean</api/data_types/boolean>` expressions evaluate to `True</api/language/true>`, then result is `True</api/language/true>`. If either expression evaluates to `False</api/language/false>` then result is `False</api/language/false>`.

The truth table for logical operators is shown below.

| Expression1 | Expression2 | And   | `Or</api/language/operators/bitwise_logical/or>` | `Xor</api/language/operators/bitwise_logical/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</api/data_types/integer>`, <span class="title-ref">And</span> returns an `integer</api/data_types/integer>` that is the result of comparing each bit of the two `integers</api/data_types/integer>` <span class="title-ref">And</span> assigning 1 to the bit position in the `integer</api/data_types/integer>` returned if both bits in the same position in the `integers</api/data_types/integer>` passed are 1. Otherwise, 0 is assigned to the bit position.

The following table gives the results for bitwise operators.

| Integer1 | Integer2 | And | `Or</api/language/operators/bitwise_logical/or>` | `Xor</api/language/operators/bitwise_logical/xor>` |
|----------|----------|-----|--------------------------------------------------|----------------------------------------------------|
| 0        | 0        | 0   | 0                                                | 0                                                  |
| 0        | 1        | 0   | 1                                                | 1                                                  |
| 1        | 0        | 0   | 1                                                | 1                                                  |
| 1        | 1        | 1   | 1                                                | 0                                                  |

The `Operator And</api/language/operators/operator_overloads/operator_and>` function can be used to define the <span class="title-ref">And</span> operator for classes.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Use within if statements

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

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

## Sample code

This example uses the <span class="title-ref">And</span> operator to perform logical comparisons.

``` xojo
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 <span class="title-ref">And</span> with Integer values:

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

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Not</api/language/operators/bitwise_logical/not>`, `Or</api/language/operators/bitwise_logical/or>`, `Xor</api/language/operators/bitwise_logical/xor>` operators; `Bitwise</api/language/bitwise>` class; `Operator And</api/language/operators/operator_overloads/operator_and>` function; `Operator precedence</api/language/operators/operator_precedence>`

</div>
