# Logic

The Logic folder in Blockly contains blocks that are used to control the flow of execution of a program.These blocks include conditional statements (such as if and else), loops (such as while and for), and logical operators (such as and, or, and not).

Logic blocks are essential for writing any complex program, as they allow you to control how the program behaves based on different conditions. For example, you can use a conditional statement to check if a variable is equal to a certain value, and then execute different code depending on the result. Or, you can use a loop to repeat a block of code multiple times until a certain condition is met.

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2FvOK9F4s7cQ0CXHcqltP3%2Fimage.png?alt=media&#x26;token=12dd834c-7392-4ea3-90f6-23830b291d16" alt=""><figcaption><p>Logic blocks</p></figcaption></figure>

### If, else, else if

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2Fg6VdPZKgepV0xoLdLiPN%2Fimage.png?alt=media&#x26;token=acd9d7e6-a123-45cb-890f-7e19ed88245c" alt=""><figcaption><p>If, else, else if block</p></figcaption></figure>

The `if`, `else`, and `else if` statements are conditional statements in programming languages. They are used to control the flow of a program by executing different blocks of code depending on the value of a condition.

The `if` statement checks if a condition is true. If the condition is true, the code inside the `if` block is executed. If the condition is false, the code inside the `else` block (if it exists) is executed.

The `else if` statement is similar to the `if` statement, but it allows you to check multiple conditions. If the first condition is false, the second condition is checked. If the second condition is also false, the third condition is checked, and so on. If all of the conditions are false, the code inside the `else` block (if it exists) is executed.

In Misty Studio you can build your own if-else function by clicking on the gear and dragging else if and else blocks under the if one.&#x20;

In this case, Misty will check if the condition in the "if-gap" (red) is true. If it is, she will do the action in the "do-gap" (green), if not she will do what's inside the "else-gap" (yellow).

### Value comparison

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2FMQnuMq5B25WbyJOJTm1X%2Fimage.png?alt=media&#x26;token=d80219aa-63a1-429c-83ea-7efdf0d2fc05" alt=""><figcaption><p>value comparison block </p></figcaption></figure>

This block allows you to compare two values. If the condition is respected the block will return *true*, otherwise it will return *false*. These are useful in multiple event scenarios when you want Misty to perform different actions based on the type of event triggered.

For example, this block could be used as a condition in the if-else block seen previously. You can select his condition by clicking on it.

### Case Comparison

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2FRd7ntNhbAQRTBLLXtPvk%2Fimage.png?alt=media&#x26;token=b1a14ff5-124e-4376-9b0a-2f6f909a98e0" alt=""><figcaption><p>case comparison block</p></figcaption></figure>

This block allows you to compare two inputs and give an output.

*And-case*: This block will return *true* if both inputs are true.

*Or-case*: This block will return *true* if at least one of the inputs is true.

### Not&#x20;

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2FD1Ti3wQwTdK9Zrqiqi4z%2Fimage.png?alt=media&#x26;token=3227a3c1-ed03-4f32-884b-f49d27aeccfb" alt=""><figcaption><p>not block</p></figcaption></figure>

With this block, you can reverse the value of the input. If the input is true it returns *false,* if the input is false it returns *true.*

### True-False

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2F36aZkY859CYsghu409Zz%2Fimage.png?alt=media&#x26;token=c2e35814-4bae-434c-a66e-dd7deb042bc1" alt=""><figcaption><p>true and false block</p></figcaption></figure>

This block returns either *true* or *false.*

In general, returning true or false is a good way to indicate the success or failure of a function, or to control the flow of a program.

### Null

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2F9QifKjz8F04v3hvut4kj%2Fimage.png?alt=media&#x26;token=1b21f591-8fd3-49d2-b397-f24ca13230c6" alt=""><figcaption><p>null block</p></figcaption></figure>

The null block is used to represent the absence of a value. It can be used in a variety of situations, such as:

* When you need to initialize a variable.
* When you need to return a value from a function, but the function does not need to return a specific value.
* When you need to pass a value to a function, but the function does not expect a specific value.

In Misty Studio this block simply returns *null.*

### Test&#x20;

<figure><img src="https://3732834919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrpyhVmLjv9Bu7tKDF8HF%2Fuploads%2FjWs07E8OlziB8DHUERD4%2Fimage.png?alt=media&#x26;token=b7352201-0ee1-4f61-a291-0182674fcb14" alt=""><figcaption><p>test block</p></figcaption></figure>

This block checks the condition in the "test-gap" (yellow), if it is true it returns the if-true value (green), if it is false it returns the if-false value (red).&#x20;


---

# 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://lessons.mistyrobotics.com/blockly-elements/basic-blocks/logic.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.
