> 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/loops.md).

# Introduction to Loops

When it comes to programming, there's often times we want a robot to do something over and over again until we tell it to do something else. It might be a movement, a continual check of a sensor, or so many different things at once.

We could attempt to program each instance manually. In fact let's see if we can have our robot move a couple of feet using just the block setting the power fully.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F5y4XR8pGQVmkwc2fcdf1%2Fimage.png?alt=media&amp;token=a10a4a9a-1922-4e1f-bcda-9a5d58c7f964" alt=""><figcaption><p>Adding multiple drive blocks to a program</p></figcaption></figure>

My OpMode is already becoming quite long yet the robot is barely moving anywhere!

The Control Hub reads through each line of code provided incredibly fast. So fast it may not even provide enough time for a motor to fully turn on before it's off to the next if there's nothing in place to allow a buffer or repeat.&#x20;

There are options for adding timers to help, but in this tutorial we're going to focus on using loops!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F7W3QpaCGbtFM6ktXhumU%2Fimage.png?alt=media&amp;token=6605f69a-f088-40e5-bff4-9591d6fdeba7" alt=""><figcaption><p>Looping a drive block so long as the OpMode is active</p></figcaption></figure>

**Loops** control the flow of a program by repeating the execution of the code within the loop until an [exit condition](#user-content-fn-1)[^1] is met.&#x20;

## Loops in Blocks

When using the recommended "BasicOpMode" sample in Blocks, the template provides a "while loop" by default that will continue after Play is pressed on the Driver Hub and until Stop is pressed.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fk6OTR5qcZK0AJeDv9Dbz%2Fimage.png?alt=media&amp;token=2ae37430-8f74-4b56-8f27-75dd4af061d4" alt=""><figcaption><p>The default loop in the BasicOpMode sample</p></figcaption></figure>

This is where most of the OpMode will live creating what's known as an iterative[^2] program structure. What matters most with this loop is the call for if `opModeIsActive`. &#x20;

{% hint style="success" %}
If students experience a problem where their code appears to start then immediately stop, double check they have a loop in their OpMode! In OnBot Java, this appears as while (opModeIsActive()) followed by the code.
{% endhint %}

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FI1P7qOnzbiRM31L8zhHM%2Fimage.png?alt=media&amp;token=789cf7f9-c03a-4c53-8050-cb2e7224895a" alt=""><figcaption><p>Call for if the opModeIsActive</p></figcaption></figure>

This call checks that the OpMode should still be running, meaning Stop has not been pressed, and should ALWAYS be included with "while loops", even if there is another condition being checked. This is intended to be a safety feature to ensure everything shuts down properly when Stop is pressed OR in the event of a disconnect/malfunction.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FCxBkh0nu3RlqGY3WKzvS%2Fimage.png?alt=media&amp;token=b4f3a255-a7b0-4cdf-8ff0-2d4387aa169f" alt=""><figcaption><p>Example of the loop checking two conditions</p></figcaption></figure>

Programs running without this check may cause an error on the Driver Hub or crash.&#x20;

## Loops Practice

In Blocks, there are a few loop options, but we'll be focusing on the options to repeat a number of times and the "while loop":

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FMN9zIfL0cQj1MAksPQ3U%2Fimage.png?alt=media&amp;token=c633a0e2-1e2f-4a83-8623-16f3fe8ede9b" alt=""><figcaption><p>Repeat and while loops in Blocks</p></figcaption></figure>

In this practice exercise, we're going to have the robot count so first we need to create a new variable called "count".

<img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FqYmBPSM845Ct9FTJdri3%2Funknown.png?alt=media&amp;token=10c14460-9ca7-49d9-abe6-16a743fec082" alt="Creating a &#x22;count&#x22; variable" height="442" width="421">

This variable needs to be to 0 during initialization before we use it in the loop.

<img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FhmJSBC6hN2vyjoErgj9a%2Funknown.png?alt=media&amp;token=f532d571-c786-49dd-8ee2-810abd2c1913" alt="Adding the &#x22;count&#x22; variable to initiliazation" height="377" width="435">

From the loop menu, we'll use the option to repeat 10 times. Then use our variable and math blocks so that the final count goes up by 1 each repeat.

<img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FzLSMavGDGdIHQsv1VAWN%2Funknown.png?alt=media&amp;token=11528ea1-c22e-43c2-9cf6-abb4b6830383" alt="Adding a repeat 10 times and math for increasing the count" height="397" width="461">

Lastly, we need to add telemetry so we can see what the count currently is on the Driver Hub's screen. The telemetry AND telemetry update blocks must be in the repeat loop.

<img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FNiZGbHtZPQv1wHrVqz4b%2Funknown.png?alt=media&amp;token=d24d43cf-660b-4b2e-9b4f-2979a77eaeaa" alt="Adding telemetry to see the current count" height="444" width="461">

Save and test the OpMode!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FDFZVK5AyQYzGmgI3fWyw%2FStop_Test.png?alt=media&amp;token=777578ba-e5bf-4c2e-946c-a86566e028d4" alt="" width="375"><figcaption></figcaption></figure>

### What happened?

Likely, you saw the number for the count rapidly increasing much more than 10 times. We can see just how fast the program cycles through each loop!

But why does it continue past 10 times?&#x20;

Let's look closer at our program. Right now we have two loops going!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F56aFZymHugf2Z2snq2wK%2Fimage.png?alt=media&amp;token=0b228c76-1a9e-4ce6-b099-afd66c80779b" alt=""><figcaption><p>The two loops: while loop and repeat 10 times</p></figcaption></figure>

Recall, our larger loop will continue until Stop is pressed since there is no other exit condition given. Each time the 10 count ends, the "while loop" starts again doing another 10 counts and again and again and again... until Stop is pressed.

To remedy this, we can tell the OpMode to stop once the 10 counts have ended.&#x20;

<img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FZOtBxzoGxVo74PlGOFkD%2Funknown.png?alt=media&amp;token=5dfb7a3f-d495-46cb-a0ae-c6f9b9bb6f49" alt="Adding a call to request the OpMode stops once the 10 counts finish" height="431" width="425">

Save the OpMode and try again!

### What happened?

The program ended... but it was so fast we could hardly see it work!&#x20;

Let's add a delay for this experiment to make it easier to watch. From the LinearOpMode menu, look for the sleep[^3] block:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FHJKe3GU9xqfoPrTYpmAm%2Fimage.png?alt=media&amp;token=e0d3d6df-b966-4616-9d7c-ba45e323fddf" alt=""><figcaption></figcaption></figure>

Add the sleep block below the telemetry update. The default is a 1 second delay, which works well for this test!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fg1MnRTdSmTV526eOtQTe%2Fimage.png?alt=media&amp;token=0455b009-1160-4204-918c-625800fbb984" alt=""><figcaption></figcaption></figure>

Save your OpMode and test a final time!

[^1]: An exit condition is a predetermined condition, such as time, that causes a loop to end.

[^2]: Iterative programs repeat!

[^3]: Sleep can be used like a timer, BUT while the program is asleep the robot will not respond to any other inputs (excluding "Stop").&#x20;
