Blocks OpMode ‐ FOR Loop to find item in List - WestsideRobotics/FTC-training GitHub Wiki

Here's a simple FTC Blocks OpMode to demonstrate finding an item in a List.

In this example, we want to know if a list contains the word "Blue". A brief explanation appears below.

In FTC Blocks, a List is created like a regular Variable. It becomes a List when assigned the output of a Block called "create list with...".

In Blocks (and Java, and other languages), a "FOR Loop" can examine or process each item in a List, one at a time, in order. Above, this is the green structure starting with "for each item i in list...".

The current item being processed has a default variable name "i" (can be changed, e.g. "thisItem"). Your OpMode can use or determine the value of "i", and take action accordingly.

When all items in the list have been processed, the "FOR Loop" ends and the OpMode continues. At that point, your OpMode could take action based on any information that was stored during looping. Here, a Boolean Variable isBlueFound was set to True, only if "Blue" was actually in the list.

Usually, a "FOR Loop" runs very fast, to quickly process a large set of values. Here, each loop was paused 2 seconds, to display info showing how the loop works.

============

The .BLK file is posted here. Save the code, open FTC Blocks, then choose "Upload OpMode".