# Conditional IF statement

#### **Checking if a variable is not empty**

{% hint style="info" %}
To verify that a variable contains a value (i.e., it is not `None` and not an empty string), you must check that it is **NOT equal** to an empty string.

**Correct formula:**

`"#{value}" != ""`

This is essential for validating data, such as ensuring a response was received from an external API call before proceeding.
{% endhint %}

### IF()

<details>

<summary>Description</summary>

**if(condition, value\_if\_true, value\_if\_false)**

**condition** - trigger

**value\_if\_true** - value if True

**value\_if\_false** - value if False

{% hint style="warning" %}
Maximum expression length: 2000 characters
{% endhint %}

</details>

<details>

<summary>Example</summary>

Let’s go through a few examples:

SILENCEDAYS\_2 = if(SILENCEDAYS\_2 == 1, 1, 0). In this case, if the variable `SILENCEDAYS_2` exists and equals 1, its value remains 1. If it doesn’t exist or has a different value, it will be set to 0. This is useful before performing mathematical operations to safeguard against empty or undefined variable values.

ClientName = if(ClientName == 1, P1, if(ClientName == 2, P2, if(ClientName == 3, P3, 7))), where P1, P2, P3 are variables.&#x20;

If ClientName equals 1, then the value P1 will be assigned;\
if ClientName equals 2, then the value P2 will be assigned;\
if ClientName equals 3, then the value P3 will be assigned;\
otherwise, the value 7 will be assigned.

As you can see, nested if constructions can be used. This is useful when you want to teach the bot to calculate the order amount, where the unit price depends on the quantity:

Order\_Amount = round(if(Quantity >=100, if(Quantity >=200, if(Quantity >=300, if(Quantity >=400, if(Quantity >=500, if(Quantity >=1000, if(Quantity >=2000, if(Quantity >=3000, if(Quantity >=5000, 25\*Quantit&#x79;*, 30\**&#x51;uantity), 35\*Quantit&#x79;*), 40\**&#x51;uantity), 45\*Quantit&#x79;*), 50\**&#x51;uantity), 55\*Quantit&#x79;*), 60\**&#x51;uantity), 65\*Quantit&#x79;*), "Cannot calculate... There was an error somewhere in your order. Please try again from the beginning.")* \* 100) / 100

<div data-with-frame="true"><figure><img src="/files/OKsbNEl29aZhzsQESX7u" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/pXgpmQYTk542REPDMuyU" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/jN2LiFW6VzFxdktzVugO" alt=""><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example for copying</summary>

<pre><code>SILENCEDAYS_2 = if(SILENCE_DAYS_2 == 1, 1, 0)
ClientName = if(ClientName == 1, Р1, if(ClientName == 2, Р2, if(ClientName == 3, Р3, 7)))
<strong>Order_Amount = round(if(Quantity >=100, if(Quantity >=200, if(Quantity >=300, if(Quantity >=400, if(Quantity >=500, if(Quantity >=1000, if(Quantity >=2000, if(Quantity >=3000, if(Quantity >=5000, 25*Quantity, 30*Quantity), 35*Quantity), 40*Quantity), 45*Quantity), 50*Количество), 55*Quantity), 60*Quantity), 65*Quantity), "Cannot calculate... There was an error somewhere in your order. Please try again from the beginning.")  * 100) / 100
</strong></code></pre>

</details>


---

# 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://docs.mavibot.ai/chatbot/functions/calculator/conditional.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.
