CPSC 110-08: Computing on Mobile Phones
Spring 2011

ListsAndLoops Part III

In this tutorial we will look at some alternative ways to construct loops in App Inventor.

The for each Loop

Recall the sumAList function we wrote for homework in the previous ListsAndLoops tutorial:

  • Write and test a function named sumAList that calculates the sum of a list of integers, aList, where aList is an argument. You may assume that the aList contains nothing but numbers. An empty list should sum to 0.
  • Here's the block that defines a solution to this exerise:

    The sumAList function takes a single argument, aList3, which specifies the list of numbers that you want to sum.

    Note how the while loop is set up:

    Initialization Step: Its initialization step consists of two operations:

    Loop Test Its loop test -- temp ≤ length of list aList3 -- tests whether the loop variable is less than or equal to the length of the list that's being summed. This loop will repeat as long as that condition is true. The loop body will simply be skipped altogether if the test is false.

    Update Step: Its update step inside the body (the 'do' slot) of the loop adds the next list item to the sum, computing a running total. Notice how temp is used to select the next list item that we want to add to sum. And it adds 1 to temp. This guarantees that the loop makes progress toward termination because temp will eventually be greater than n1, which will cause the loop to stop.

    The Foreach Pattern

    Basically, this loop perform an operation for each value in a list. This is an example of a for each list item pattern. Because this pattern is such a common type of code block, App Inventor includes a foreach block that makes it easy to code this pattern:

    The for each block has 2 slots that must be filled in besides the do slot:

    If you correctly specify these two slots, App Inventor will take care of the rest. For each value in the list, it will select that value and set it to variable. It will then peform the operations in the do slot where that value list value can be used.

    Given this new control pattern, here is how we would rewrite the sumAList function using a for each loop.

    In this example, we still need to initialize the sum, our running total, to 0. But the for each loop takes care of selecting each number from aList31 and adding it to sum.

    Download Source

    Click here to download the source code and use it for the following exercises.

    In-class Exercises, and Homework

    Working in pairs, perform each of the following exercises. If you do not finish in class, do the exercises for homework. Solutions next class. Each student should create a portfolio page for this exercise with solutions and commentary.

    1. Write and test a function named filterOutZeroes that uses a for each loop to remove all the 0s from a list of numbers. For example, if the original list is (0 1 0 0 2 0 2), then your function should generate the list (1 2 2).