# If Statements

In the OSL language, `if` statements are pivotal for controlling the flow of a program based on certain conditions. Below is a breakdown of how `if` statements work:

## **Basic `if` Statement:**

The basic `if` statement tests a condition. If the condition is true, the commands within the block are executed. If not, the program moves to the next section.

{% tabs %}
{% tab title="OSL" %}

```javascript
if 10 > 5 (
   log "10 is bigger than 5!"
)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
if (10 > 5) {
   console.log("10 is bigger than 5!")
}
```

{% endtab %}

{% tab title="Python" %}

```python
if 10 > 5:
   print("10 is bigger than 5!")
```

{% endtab %}
{% endtabs %}

## if else Statements

`if-else` statements allow for alternative code blocks to be executed if the condition is false.

**`if-else` Statement:**

If the condition in the `if` statement is true, the commands within the first block are executed. Otherwise, the commands within the `else` block are executed.

Example:

```js
if condition (
   say "command1"
   say "command2"
) else (
   say "command3"
   say "command4"
)
```

## else if Statements

`else-if` statements allow for multiple conditions to be checked sequentially. If one condition is true, the corresponding block of code is executed.

**`else-if` Statement:**

Multiple conditions can be checked using `else-if`. If the condition in the `if` statement is false, the program checks the condition in the `else-if`. If that's true, the corresponding block of commands is executed.

Example:

```js
if condition1 (
   say "command1"
   say "command2"
) else if condition2 (
   say "command3"
   say "command4"
)
```

## Short Forms

Short forms of `if` and `if-else` statements can be used for concise syntax.

### **Short Form of if Statement:**

```js
if condition (;command1;)
```

```js
test_var = "hi"
if 10 > 3 (;say test_var;)
// you can use ; <== to insert a newline into the script after compilation
```

### **Short Form of if-else Statement:**

```js
if condition (;command1;) else (;command2;)
```

#### Additional Information on Conditional Statements (if):

* Conditions can involve logical operators (`and`, `or`, `not`) and comparison operators (`==`, `~=`, `<`, `>`, `<=`, `>=`).
* Conditions can also include function calls that return boolean values.
* `if` statements can be nested within each other to create more complex decision-making structures.

Now, let's look at examples for each type of statement:

## Examples:

### **Basic `if` Statement:**

```js
// Example of a basic if statement
// If the temperature is greater than 25, it's considered a hot day
temperature = 30
if temperature > 25 (
    say "It's a hot day!"
)
```

In this example, the `if` statement checks if the `temperature` variable is greater than 25. If the condition is true, the program executes the commands inside the block, which outputs "It's a hot day!" using the `say` command.

### **`if else` Statement:**

```js
// Example of an if-else statement
// Determines if a person is an adult or a minor based on their age
age = 17

if age >= 18 (
    say "You are an adult."
) else (
    say "You are a minor."
)
```

In this example, the `if-else` statement checks if the `age` variable is greater than or equal to 18. If the condition is true, it outputs "You are an adult." If the condition is false, it outputs "You are a minor."

### **`else if` Statement:**

```js
// Example of an else-if statement
// Assigns a letter grade based on a numerical grade
grade = 85

if grade >= 90 (
    say "You got an A."
) else if grade >= 80 (
    say "You got a B."
) else if grade >= 70 (
    say "You got a C."
) else (
    say "You need to improve your grade."
)
```

In this example, the `else-if` statement checks multiple conditions to determine the letter grade based on the `grade` variable. If the grade is greater than or equal to 90, it outputs "You got an A." If the grade is between 80 and 89, it outputs "You got a B." If the grade is between 70 and 79, it outputs "You got a C." Otherwise, it outputs "You need to improve your grade."


---

# 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://osl.mistium.com/program-flow/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.
