How do you do something to every item in a list?

Data lists are found in many apps. When you use Facebook, for instance, there is a list of your friends, a list of your status updates, etc. In your own app, you might keep track of your friends phone numbers, a list of your past scores in a game, or the number of miles you ran each day last week. With just about any software, there is probably data lists involved. App Inventor, like most languages, provides a way to process the items of a list, to perform the same operation on each item. With App Inventor, you typically use a for each in list block. Check out the following examples:

Example 1. How do you send a text to a list of your friends?

example 2

The list phoneNumberList is defined at the top. It has “concrete” data (e.g., the three fictitious phone numbers). Most apps store dynamic, user-generated data, but for this example we’ll focus on the processing when you have a list.

The blocks inside the for each block are repeated for each item in the given list phoneNumberList. In this case, there are three items, so the three inner blocks will be repeated three times. Nine total blocks will be executed (“executed” is the computer science term for performing the operation that the block defines. Don’t worry, nobody is being killed!). The text “Missing you” will be sent to all three numbers. The for each in list is called a “loop” because the app loops up to the top of the for each after executing the bottom inner block. In computer science terminology, we say the app iterates through the items.

The parameter item defined in the for each is a placeholder and always holds the current item being processed. The first time through the loop it is “111-1111”. The second time through it is “222-2222”, and the third time it is “333-3333”. Thus all the numbers are sent the message.