> For the complete documentation index, see [llms.txt](https://docs.revrobotics.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.revrobotics.com/rev-professional-development-technical-path/blocks-boot-camp/logic.md).

# Introduction to Logic

## What is Logic?

In programming, **logic** refers to a set of rules we create within the code for the robot to follow. It allows the robot to make decisions based on the information it gathers rather than going only through a single step-by-step process of execution.&#x20;

In Blocks, the Logic menu includes options to create comparison statements, like to check if one thing is greater than the other, and the option to make something the opposite with the "not" block. But for this section we'll be focusing on if/else Statements!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FNmPe7LosaPoPVXEGRKOF%2Fimage.png?alt=media&amp;token=cefafdb1-efb2-41a3-9db5-f9886a1fbe5d" alt=""><figcaption><p>Example of Logic Blocks in Use</p></figcaption></figure>

### If/Else Statements

One of the key concepts of logic in programming are if/else statements, also referred to as if/then statements.&#x20;

In its most simple format an if/else statement asks the robot to check **IF** something is happening and if the answer is yes, or true in the robot's mind, **THEN** it will **DO** what has been asked.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FjgU2fhTOFyTvUNiNTbPo%2Fimage.png?alt=media&amp;token=cdd4f638-b89d-41e0-87a5-b116a2d1237d" alt=""><figcaption><p>Simplest If/Else Statement</p></figcaption></figure>

In this format, the robot hasn't been told what to do if the statement is false. That's where the **else** comes in to play. Same as before the robot checks **IF** something is true, and if so, **THEN** executes a command, **ELSE** it executes a different command.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FeuGaAHkM73IQwfh4BHyw%2Fimage.png?alt=media&amp;token=a0a79d70-04a5-4ffa-8407-d164a25d5889" alt=""><figcaption><p>Full If/Else Statement</p></figcaption></figure>

Let's look at an example. Looking at the code below, the robot is checking **IF** the gamepad's A button has been pressed. If the check returns true, meaning the button is pressed, **THEN** it will spin the servo at a power of 1, but if it returns false the servo's power will be set to 0.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F3Isp67l3mj7hBVlxmPCh%2Fimage.png?alt=media&amp;token=45255978-aecd-4fd9-a56f-45dc372f17ce" alt=""><figcaption><p>If the gamepad button A is pressed then turn on the servo, else turn off the servo.</p></figcaption></figure>

### Else If

Multiple things can be checked at once, such as various buttons on a gamepad, by expanding the statement to include an **else if**:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FL9IFL5bAbs1f5SEefaVR%2Fimage.png?alt=media&amp;token=2490a51e-cbc9-456a-861a-86d6cb2f0927" alt=""><figcaption><p>If/else else if statement</p></figcaption></figure>

By adding an **else if**, our robot now has multiple things to check for if it's true or false.&#x20;

In the following example, we've added to our check for if A is pressed on the gamepad to alternatively check if B is pressed. **IF** B is pressed, **THEN** the servo will spin in reverse at -1 power. Our else remains the same so the servo turns off if neither button is pressed.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FTnXB9oSQWWpjNFCx2mrK%2Fimage.png?alt=media&amp;token=83428ce2-8f52-4b5e-935f-8adee1ccf2a6" alt=""><figcaption><p>If A is pressed, spin the servo. Else if B is pressed, spin the servo in reverse.</p></figcaption></figure>

Try creating this program yourself and give it a try!&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fd58251MJSsqutfpLupRh%2Fimage.png?alt=media&amp;token=319e6e39-1ffd-4784-97a8-0b3c7debd909" alt=""><figcaption><p>Full If/Else Example OpMode</p></figcaption></figure>

**Consider:**

* What happens when A or B is pressed?
* What happens if both are pressed at the same time?
* What happens if we control a different actuator with B?

{% hint style="info" %}
Your servo name may differ than the example, which shows as crServo from the configuration.&#x20;
{% endhint %}

### Editing If/Else Statements in Blocks

In Blocks, there are only a few if/else statements available by default:&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FVU2AxZgnYVJa3Gl4nsHn%2Fimage.png?alt=media&amp;token=45463a3a-9574-471e-84e1-2b5e982721da" alt=""><figcaption><p>Blocks Logic Menu</p></figcaption></figure>

But, if we need something different then we can click the blue gear icon to customize the if/else block!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FqtvJcbPMC4zs6eFZzNma%2Fimage.png?alt=media&amp;token=ac3263cb-bf79-486f-84b7-a77a3b540ddb" alt=""><figcaption><p>The menu for the if/else statements</p></figcaption></figure>

We can add multiple else if options to expand the statement as long as we need.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FGNJhVXYv1Y2P9FYFgOMZ%2Fimage.png?alt=media&amp;token=1e32a409-3957-4cae-adbf-a5f8a04e262b" alt=""><figcaption><p>Expanding if/else statements</p></figcaption></figure>

Keep in mind, there's appropriate times for when to have checks all together in one statement and when to have multiple individual statements. What works best will vary per project, person, and how an OpMode is organized.&#x20;
