> For the complete documentation index, see [llms.txt](https://pretender.gitbook.io/beginners-scripting-guide-beta/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pretender.gitbook.io/beginners-scripting-guide-beta/if-statement.md).

# If statement

✏ Beginners Scripting Guide ✏

In this section, we will discuss how if statement works. An if statement is a code of block that will be executed whenever the if statement's condition is met.

{% hint style="info" %}
**NOTE:** The parentheses can be used to group multiple conditions and the "**not**" term will be used to flip the current value to the opposite of the current value.
{% endhint %}

### **CONDITION OPERATIONS**

* \>= (Condition 1 greater and equal to condition 2)
* <= (Condition 1 less and equal to condition 2)
* \~= (Condition 1 is not condition 2)
* \== (Condition 1 is equal to condition 2)

### **First example:**

```lua
if 1 > 0 then -- This will check if 1 > 0 or not.
    -- Code block in here will be executed when condition(s) are/is met  
    print('1 is greater than 0')
end
```

### **Second example:**

```lua
if not 1 > 3 then -- This will check if 1 > 3.
    -- The statement is false but we use the "not" to flip that false to the opposite value which is true.
    print('1 is greater than 0')
end
```

<mark style="color:red;">**and**</mark> can be used to combine multiple statements together

```lua
if 3 > 1 and 3 > 2 then -- Code block here will be executed when both of the conditions are met.
    -- The engine will check if 3 > 1 and if it's true then the engine will move on to the next statement.
    -- Now the engine will check if 3 > 2 and if it's true then the engine will execute the code block.
    -- From this we can see that the engine will execute the code block if both of the statements are true.
    print('3 is greater than 1 and 3 is greater than 2') 
end
```

<mark style="color:red;">**or**</mark> evaluates to true if either one of the statement is true&#x20;

```lua
if 3 > 1 or 3 > 5 then -- Code block here will be executed if one of the condition is met.
    -- The engine will check if 3 > 1 and if it's true or not, The engine will move on to the next statement which is 3 > 5.
    -- The engine will check 3 > 5 and if it's true or not.
    -- If one of the statements is true then the engine will execute the code block.
    print("3 > 1 or 3 > 5")
end
```

<mark style="color:red;">**elseif**</mark> can be used to extend the if statement.

<pre class="language-lua"><code class="lang-lua">local a = 2
<strong>
</strong><strong>if a > 1 then -- Trigger block code below if "a" is greater than 1.
</strong>    print('3 is greater than 1.')
elseif a &#x3C; 5 then -- Trigger block code below if "a" is less than 5.
    print('3 is less than 5.')
end
</code></pre>

<mark style="color:red;">**else**</mark> can be used to check if the condition is not true or opposite.

```lua
local a = 2

if a > 2 then
    print('A value is greater than 2') -- This will be print if "a" is greater to 2.
else
    print('A value is less or equal to 2') -- This will be print if "a" is less or equal to 2.
end
```
