# Instrução condicional IF

#### **Verificando se uma variável não está vazia**

{% hint style="info" %}
Para verificar se uma variável contém um valor (ou seja, ela não está `None` e não é uma string vazia), você deve verificar se ela é **NÃO igual** a uma string vazia.

**Fórmula correta:**

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

Isso é essencial para validar dados, como garantir que uma resposta foi recebida de uma chamada de API externa antes de prosseguir.
{% endhint %}

### IF()

<details>

<summary>Descrição</summary>

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

**condition** - gatilho

**value\_if\_true** - valor se True

**value\_if\_false** - valor se False

{% hint style="warning" %}
Comprimento máximo da expressão: 2000 caracteres
{% endhint %}

</details>

<details>

<summary>Exemplo</summary>

Vamos passar por alguns exemplos:

SILENCEDAYS\_2 = if(SILENCEDAYS\_2 == 1, 1, 0). Neste caso, se a variável `SILENCEDAYS_2` existir e for igual a 1, seu valor permanecerá 1. Se ela não existir ou tiver um valor diferente, será definida como 0. Isso é útil antes de realizar operações matemáticas para se proteger contra valores de variáveis vazios ou indefinidos.

ClientName = if(ClientName == 1, P1, if(ClientName == 2, P2, if(ClientName == 3, P3, 7))), onde P1, P2, P3 são variáveis.&#x20;

Se ClientName for igual a 1, então o valor P1 será atribuído;\
se ClientName for igual a 2, então o valor P2 será atribuído;\
se ClientName for igual a 3, então o valor P3 será atribuído;\
caso contrário, o valor 7 será atribuído.

Como você pode ver, construções if aninhadas podem ser usadas. Isso é útil quando você quer ensinar o bot a calcular o valor do pedido, onde o preço unitário depende da quantidade:

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;*), "Não é possível calcular... Houve um erro em algum lugar no seu pedido. Por favor, tente novamente desde o início.")* \* 100) / 100

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

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

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

</details>

<details>

<summary>Exemplo 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), "Não é possível calcular... Houve um erro em algum lugar no seu pedido. Por favor, tente novamente desde o início.")  * 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/pt/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.
