# Instrucción condicional IF

#### **Comprobando si una variable no está vacía**

{% hint style="info" %}
Para verificar que una variable contiene un valor (es decir, que no está `None` y no es una cadena vacía), debes comprobar que sea **DISTINTO de** una cadena vacía.

**Fórmula correcta:**

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

Esto es esencial para validar datos, por ejemplo, asegurarse de que se recibió una respuesta de una llamada a una API externa antes de continuar.
{% endhint %}

### IF()

<details>

<summary>Descripción</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" %}
Longitud máxima de la expresión: 2000 caracteres
{% endhint %}

</details>

<details>

<summary>Ejemplo</summary>

Veamos algunos ejemplos:

SILENCEDAYS\_2 = if(SILENCEDAYS\_2 == 1, 1, 0). En este caso, si la variable `SILENCEDAYS_2` existe y es igual a 1, su valor permanece en 1. Si no existe o tiene un valor diferente, se establecerá en 0. Esto es útil antes de realizar operaciones matemáticas para protegerse contra valores de variables vacíos o indefinidos.

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

Si ClientName es igual a 1, entonces se asignará el valor P1;\
si ClientName es igual a 2, entonces se asignará el valor P2;\
si ClientName es igual a 3, entonces se asignará el valor P3;\
de lo contrario, se asignará el valor 7.

Como puedes ver, se pueden usar construcciones if anidadas. Esto es útil cuando quieres enseñar al bot a calcular el importe del pedido, donde el precio unitario depende de la cantidad:

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;*), "No se puede calcular... Hubo un error en algún lugar de tu pedido. Por favor, inténtalo de nuevo desde el principio.")* \* 100) / 100

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

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

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

</details>

<details>

<summary>Ejemplo de código para copiar</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), "No se puede calcular... Hubo un error en algún lugar de tu pedido. Por favor, inténtalo de nuevo desde el principio.")  * 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/doc/es/chatbot/functions/calculadora/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.
