# Conditionals

Conditionals allow you to select different values depending on an expression. Dasel v3 supports an `if/elseif/else` block syntax and a ternary operator (`?:`).

***

### Syntax

```
if (<condition>) { <then> } else { <else> }
```

* `<condition>` must evaluate to a boolean.
* `<then>` is evaluated if the condition is true.
* `<else>` is evaluated if the condition is false.
* An `else` branch is always required.

***

### Basic Example

**Input JSON**

```json
{
  "foo": {
    "bar": "baz",
    "bong": "selected",
    "qux": "not-selected"
  }
}
```

**Query**

```bash
echo '{"foo":{"bar":"baz","bong":"selected","qux":"not-selected"}}' | dasel -i json 'foo.if (bar == "baz") { bong } else { qux }'
```

**Output**

```
"selected"
```

***

### Elseif Chains

Use `elseif` to chain multiple conditions. You can use as many `elseif` branches as needed.

```
if (<condition1>) { <result1> } elseif (<condition2>) { <result2> } else { <default> }
```

#### Example

**Input JSON**

```json
{ "score": 75 }
```

**Query**

```bash
echo '{"score": 75}' | dasel -i json '
  if (score >= 90) { "A" }
  elseif (score >= 80) { "B" }
  elseif (score >= 70) { "C" }
  else { "F" }
'
```

**Output**

```
"C"
```

#### Fizzbuzz with elseif

Given `numbers.json`:

```json
{ "numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] }
```

```bash
$ cat numbers.json | dasel -i json 'numbers.map(
    if ($this % 3 == 0 && $this % 5 == 0) {
        "fizzbuzz"
    } elseif ($this % 5 == 0) {
        "buzz"
    } elseif ($this % 3 == 0) {
        "fizz"
    } else {
        $this
    }
)'
```

***

### Literal Results

Both branches can return literal values, not just field lookups.

**Input JSON**

```json
{ "count": 7 }
```

**Query**

```bash
echo '{"count": 7}' | dasel -i json 'if (count > 5) { "many" } else { "few" }'
```

**Output**

```
"many"
```

***

### Nested Conditionals

Conditionals can be nested in the `else` branch.

```bash
echo '{"status": "pending"}' | dasel -i json '
  if (status == "active") { "go" }
  else { if (status == "pending") { "wait" } else { "stop" } }
'
```

For multi-branch cases, `elseif` is cleaner than nesting.

***

## Ternary Operator

The ternary operator provides a compact inline syntax for conditionals.

### Syntax

```
<condition> ? <then> : <else>
```

* `<condition>` must evaluate to a boolean.
* `<then>` is returned when the condition is true.
* `<else>` is returned when the condition is false.

### Example

**Input JSON**

```json
{ "age": 25 }
```

**Query**

```bash
echo '{"age": 25}' | dasel -i json 'age >= 18 ? "adult" : "minor"'
```

**Output**

```
"adult"
```

### Nested Ternary

Parentheses can be used to nest ternary expressions.

```bash
echo '{}' | dasel -i json 'true ? (false ? "a" : "b") : "c"'
```

**Output**

```
"b"
```

***

### Notes

* An `else` branch is always required — both `if/else` and `if/elseif/else` must have a final `else`.
* Both branches must return a value — you cannot have an empty branch.
* Use `elseif` (one word, no space) for chained conditions.
* Parentheses around the condition are required for `if`/`elseif` blocks but not for the ternary operator.
* The ternary operator always requires both `?` and `:` parts.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://daseldocs.tomwright.me/syntax/conditionals.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
