For the complete documentation index, see llms.txt. This page is also available as Markdown.

Advanced Loops

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

Counting loop code from the last section

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

OpMode with the loop code removed

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.

Adding the "and" statement to the loop

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:

Check if "count" is less than or equal to 10

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

The loop will check if "count" is less or equal to 10 AND if the opModeIsActive

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.

Save the OpMode and test!

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.

Try setting the goal to different numbers!

Last updated

Was this helpful?