# Logic

Aerospike logic expressions allow you to apply logical operators to boolean expressions, returning a boolean result.

This guide covers operators like `and`, `or`, `not`, and `exclusive` for combining or evaluating conditions within queries. Code examples show how to use these expressions to simplify complex logic and improve query performance.

## Ops

#### `and`

```python
and(arg0, arg1, ...)
```

Description: Returns `false` if any ‘boolean\_expr’ is `false`; otherwise, it returns `true`.

Arguments: | Name | Type |
| --- | --- |
| `arg0` | `boolean_expr` |
| `arg1` | `boolean_expr` |
| `...` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.2.0.4

Example: Find records have a key and the key is less than 1000.

-   [c](#tab-panel-583)
-   [java](#tab-panel-584)

```c
as_exp_build(predexp,

  as_exp_and(as_exp_key_exists(),

    as_exp_cmp_lt(as_exp_int_key(), as_exp_int(1000))));
```

```java
Expression exp = Exp.build(

  Exp.and(Exp.keyExists(),

    Exp.lt(Exp.key(Exp.Type.INT), Exp.val(1000))));
```

---

#### `exclusive`

```python
exclusive(arg0, arg1, ...)
```

Description: Returns `true` if exactly one ‘boolean\_expr’ is `true`; otherwise, it returns `false`. This expression is helpful for testing whether multiple expressions are mutually exclusive.

Arguments: | Name | Type |
| --- | --- |
| `arg0` | `boolean_expr` |
| `arg1` | `boolean_expr` |
| `...` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.6.0

Example: Find records where only one of the following criteria is true: the value in bin ‘hand’ is ‘hook’, the value in bin ‘leg’ is ‘peg’, or the value bin ‘pet’ is ‘parrot’.

-   [c](#tab-panel-587)
-   [java](#tab-panel-588)

```c
as_exp_build(predexp,

  as_exp_exclusive(

    as_exp_cmp_eq(as_exp_bin_str("hand"), as_exp_str("hook")),

    as_exp_cmp_eq(as_exp_bin_str("leg"), as_exp_str("peg")),

    as_exp_cmp_eq(

      as_exp_bin_str("pet"), as_exp_str("parrot"))));
```

```java
Expression exp = Exp.build(

  Exp.exclusive(Exp.eq(Exp.stringBin("hand"), Exp.val("hook")),

    Exp.eq(Exp.stringBin("leg"), Exp.val("peg"))

    Exp.eq(Exp.stringBin("pet"), Exp.val("parrot"))));
```

---

#### `not`

```python
not(arg)
```

Description: Returns `true` if the ‘boolean\_expr’ is `false`; otherwise, it returns `false`.

Arguments: | Name | Type |
| --- | --- |
| `arg` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.2.0.4

Example: Find where records that do not have a stored key.

-   [c](#tab-panel-585)
-   [java](#tab-panel-586)

```c
as_exp_build(predexp, as_exp_not(as_exp_key_exists()));
```

```java
Expression exp = Exp.build(Exp.not(Exp.keyExists()));
```

---

#### `or`

```python
or(arg0, arg1, ...)
```

Description: Returns `true` if any ‘boolean\_expr’ is `true`; otherwise, it returns `false`.

Arguments: | Name | Type |
| --- | --- |
| `arg0` | `boolean_expr` |
| `arg1` | `boolean_expr` |
| `...` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.2.0.4

Example: Find records where bin ‘country’ is either ‘US’ or ‘CA’.

-   [c](#tab-panel-589)
-   [java](#tab-panel-590)

```c
as_exp_build(predexp,

  as_exp_or(

    as_exp_cmp_eq(as_exp_bin_str("country"), as_exp_str("US")),

    as_exp_cmp_eq(

      as_exp_bin_str("country"), as_exp_str("CA"))));
```

```java
Expression exp = Exp.build(

  Exp.or(Exp.eq(Exp.stringBin("country"), Exp.val("US")),

    Exp.eq(Exp.stringBin("country"), Exp.val("CA"))));
```

---