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

# Advanced Loops

Looking back at our loop program, we have the program end after the inner loop repeats 10 times.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FJWA1varwxU9uU1bwy3jO%2Fimage.png?alt=media&amp;token=95678e4d-96cc-4eca-a112-8ac97125f619" alt="" width="453"><figcaption><p>Counting loop code from the last section</p></figcaption></figure>

What if we instead set up the "while loop" to continue until the count is equal to 10 or another number?

We need to always have the call for opModeIsActive, but that doesn't mean we can't add a second exit condition!

### Adding an Exit Condition

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FvofV6FAmoWIeQm23AKZu%2Fimage.png?alt=media&amp;token=e496bbeb-43f8-46a1-bd61-f85525162bd4" alt=""><figcaption><p>OpMode with the loop code removed</p></figcaption></figure>

To start, I removed the existing code from the "while loop". Next I need to find the "and" option from the Logic menu. This will snap to the "while loop" with the call if opModeIsActive being added to one side.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FHWSAv406LO6lZwAAxoSY%2Fimage.png?alt=media&amp;token=82aec406-459e-4ece-9693-78892ad0479a" alt=""><figcaption><p>Adding the "and" statement to the loop</p></figcaption></figure>

Let's start by having the robot check if the opModeIsActive AND if the count variable currently equals 10 or less. We can use blocks from the Logic and Math menus to create our statement:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F2dFy43OeExNmUSotMxQn%2Fimage.png?alt=media&amp;token=0d6ff43b-add1-4ceb-83dd-1c7f6e48b2e0" alt=""><figcaption><p>Check if "count" is less than or equal to 10</p></figcaption></figure>

Then add this to the "and" statement attached to the loop:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FtNda3sarBa9pZQPmYJ6w%2Fimage.png?alt=media&amp;token=2742c852-c257-4ed7-ab51-5413441ec2dd" alt=""><figcaption><p>The loop will check if "count" is less or equal to 10 AND if the opModeIsActive</p></figcaption></figure>

All that's left is to add the same set up we had before so that with each loop the count increases by 1! Remember a program cycles incredibly fast, so we'll add our sleep block again to help be able to see what happens.&#x20;

Save the OpMode and test!

<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>

{% hint style="info" %}
The telemetry will show the count = 11 before stopping due to the current logic checking for if the variable is equal to or less than 10.&#x20;
{% endhint %}

Try setting the goal to different numbers!&#x20;
