# Declare and control-flow

Aerospike expressions for declaring variables enable more advanced control-flow within database queries.

This guide explains how to define and use variables using expressions like `cond`, `unknown`, `let`, `def`, and `var`, allowing you to evaluate conditions, manage control-flow, and reuse values efficiently. These expressions can simplify complex logic and improve query performance.

## Ops

#### `cond`

```python
cond(condition0, action0, condition1, action1, ..., default-action)
```

Description: Takes a set of test-expression/action-expression pairs. It evaluates each test one at a time. If a test returns `true`, ‘cond’ evaluates and returns the value of the corresponding expression and doesn’t evaluate any of the other tests or expressions. The final expression is the default expression and is evaluated if all tests evaluate to `false` All actions-expressions must evaluate to the same type or be the `unknown` expression.

Arguments: | Name | Type |
| --- | --- |
| `condition0` | `boolean-expr` |
| `action0` | `expr` |
| `condition1` | `boolean-expr` |
| `action1` | `expr` |
| `...` | `expr` |
| `default-action` | `expr` |

Returns: `expr`

Introduced: 5.6.0

Example: Convert the value in bin “grade” from a number to a letter grade.

-   [c](#tab-panel-565)
-   [java](#tab-panel-566)

```c
as_exp_build(write_exp,

  as_exp_cond(

    as_exp_cmp_ge(

      as_exp_bin_float("grade"), as_exp_float(90.0)),

    as_exp_str("A"),

    as_exp_cmp_ge(

      as_exp_bin_float("grade"), as_exp_float(80.0)),

    as_exp_str("B"),

    as_exp_cmp_ge(

      as_exp_bin_float("grade"), as_exp_float(70.0)),

    as_exp_str("C"),

    as_exp_cmp_ge(

      as_exp_bin_float("grade"), as_exp_float(60.0)),

    as_exp_str("D"),

    as_exp_str("F")));
```

```java
Expression writeExp = Exp.build(

  Exp.cond(

    Exp.ge(Exp.floatBin("grade"), Exp.val(90.0)), Exp.val("A"),

    Exp.ge(Exp.floatBin("grade"), Exp.val(80.0)), Exp.val("B"),

    Exp.ge(Exp.floatBin("grade"), Exp.val(70.0)), Exp.val("C"),

    Exp.ge(Exp.floatBin("grade"), Exp.val(60.0)), Exp.val("D"),

  Exp.val("F")));
```

---

#### `def`

```python
def(name, value)
```

Description: Defines a variable within a [`let`](#let) statement.

Arguments: | Name | Type |
| --- | --- |
| `name` | `string-value` |
| `value` | `expr` |

Returns: `expr`

Introduced: 5.6.0

Example: See the example for the [`let`](#let) expression.

---

#### `let`

```python
let(def(...), def(...), ..., expr)
```

Description: Defines variables to be used within the `let` expression’s scope. The `let` expression returns the evaluated result of the last argument. This expression is useful if you need to reuse the result of a complicated or expensive expression.

Arguments: | Name | Type |
| --- | --- |
| `def(...)` | `def-expr` |
| `def(...)` | `def-expr` |
| `...` | `def-exprs` |
| `expr` | `expr` |

Returns: `expr`

Introduced: 5.6.0

Example: Find records where the count of hll-bin “cookies” is less than 1,000 or greater than 100,000,000.

-   [c](#tab-panel-561)
-   [java](#tab-panel-562)

```c
as_exp_build(predexp,

  as_exp_let(

    as_exp_def("count",

      as_exp_hll_get_count(as_exp_bin_hll("cookies"))),

    as_exp_or(

      as_exp_cmp_lt(as_exp_var("count"), as_exp_int(1000)),

      as_exp_cmp_gt(

        as_exp_var("count"), as_exp_int(100000000)))));
```

```java
Expression exp = Exp.build(

  Exp.let(

    Exp.def("count",

      Exp.hllGetCount(as_exp_bin_hll("cookies"))),

    Exp.or(

      Exp.lt(Exp.var("count"), Exp.val(1000)),

      Exp.gt(Exp.var("count"), Exp.val(100000000)))));
```

---

#### `unknown`

```python
unknown()
```

Description: Allows you to abort an operation-expression (‘write-exp’ or ‘read-exp’). This expression returns a special ‘unknown’ trilean value which, when returned by an operation-expression, will result in an error code 26 (not applicable). These failures can be ignored with the ‘NO\_EVAL\_FAIL’ policy, which allows the subsequent operations in the transaction to proceed.  
  
This expression is only useful from an [`cond`](#cond) expression within an operation-expression and should be avoided in filter-expressions, where it might trigger an undesired move into the storage-data phase. If a ‘test’ expression within a `cond` expression yields the special ‘unknown’ trilean value, then the `cond` also immediately yields the ‘unknown’ value and does not evaluate any more ‘test’ expressions.  
  
This special ‘unknown’ trilean value is the same value returned by any failed expression. For example the expression `exp_div(1, 0)` results in the ‘unknown’ value because integer division by zero is illegal.

Returns: `unknown`

Introduced: 5.6.0

Example: Add 1 to the value in the bin “count” unless the count is greater than or equal to 10.

-   [c](#tab-panel-563)
-   [java](#tab-panel-564)

```c
as_exp_build(write_exp,

  as_exp_cond(

    as_exp_cmp_lt(as_exp_bin_int("count"), as_exp_int(10)),

    as_exp_add(as_exp_bin_int("count"), as_exp_int(1)),

    as_exp_unknown()));
```

```java
Expression writeExp = Exp.build(

  Exp.cond(

    Exp.lt(Exp.intBin("count"), Exp.val(10)),

    Exp.add(Exp.intBin("count"), Exp.val(1)),

    Exp.unknown()));
```

---

#### `var`

```python
var(name)
```

Description: Retrieve the value from a variable that was defined within a [`let`](#let) expression.

Arguments: | Name | Type |
| --- | --- |
| `name` | `string-value` |

Returns: `expr`

Introduced: 5.6.0

Example: See the example for the [`let`](#let) expression.

---