# If statements

If statements are very useful, however they do open a section. And opening sections means nesting, which isn't always ideal. To avoid nesting when applicable, you can change numerous if statements into multiple conditions, or an if all.

{% hint style="info" %}
See the [Checks page](/syntask/limiters/conditionals.md) for information about conditions and if alls.
{% endhint %}

Here's a few examples:

{% code overflow="wrap" lineNumbers="true" %}

```java
on place of diamond ore:
  if (type of player's helemt) = diamond helemet:
    if (type of player's chestplate) = diamond chestplate:
      if (type of player's leggings) = diamond leggings:
        if (type of player's boots) = diamond boots:
          drop (1 of diamond) at event-location

        else:
          send "You are not wearing a complete set of diamond armour!" to player
      else:
        send "You are not wearing a complete set of diamond armour!" to player
    else:
      send "You are not wearing a complete set of diamond armour!" to player
  else:
    send "You are not wearing a complete set of diamond armour!" to player
```

{% endcode %}

Here, the main piece of code is nested in five levels, which is a little ridiculous. You can convert nested if statements like this into an if all:

{% code overflow="wrap" lineNumbers="true" %}

```java
on place of diamond ore:
  if all:
    (type of player's helemt) = diamond helemet
    (type of player's chestplate) = diamond chestplate
    (type of player's leggings) = diamond leggings
    (type of player's boots) = diamond boots
  then:
    drop (1 of diamond) at event-location

  else:
    send "You are not wearing a complete set of diamond armour!" to player
```

{% endcode %}

If you don't need an else after this, you can also just use plain conditions:

{% code overflow="wrap" lineNumbers="true" %}

```java
on place of diamond ore:
  (type of player's helemt) = diamond helemet
  (type of player's chestplate) = diamond chestplate
  (type of player's leggings) = diamond leggings
  (type of player's boots) = diamond boots
  drop (1 of diamond) at event-location
```

{% endcode %}

{% hint style="info" %}
Guard clauses are also a handy style of if statements to employ, check out the [Guard Clauses page](https://x8ight.gitbook.io/syntask/conventions/never-nesting/guard-clauses) for more.
{% endhint %}


---

# 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://x8ight.gitbook.io/syntask/organisation/never-nesting/if-statements.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.
