CPSC 110-08: Computing on Mobile Phones
Spring 2011

ListsAndLoops Part II

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

The forrange Loop

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

  • Write and test a function named getNumberSequence that uses a while loop to generate the numbers 1 to n where n is an argument. For example, if size is 5, then your function should generate the list (1 2 3 4 5).
  • Here's the block that defines a solution to this exerise:

    The genNumberSequence function takes a single argument, n1, which specifies both the length of the list and the value of the last item in the sequence 1, 2, 3, ..., n1.

    Note how the while loop is set up:

    Initialization Step: Its initialization step consists of two operations:

    Loop Test Its loop test -- temp ≤ n1 -- tests whether the loop variable is less than or equal to n1, the desired length of the list. 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 number to the list and increases the value of the loop variable, temp by 1. This will guarantee that the loop makes progress toward termination because temp will eventually be greater than n1, which will cause the loop to stop.

    Basically, this loop iterates through the range of numbers 1 to n1. This is an example of a forrange pattern. Because this pattern is such a common type of code block, App Inventor includes a for range block that makes it easy to code this pattern:

    The for range block has 4 slots that must be filled in.

    If you correctly specify these four slots, App Inventor will take care of the rest. It will initialize the variable to start. It will test that variable ≤ end. And it will make progress toward termination by adding step to variable.

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

    In this example, the variable rangeValue replaces temp in the while loop. It ranges between 1 and n12, increasing by 1 each time through the loop.

    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 getOddNumberSequence that uses a for range loop to generate the odd numbers between 1 to n, where n is an argument. For example, if n is 10, then your function should generate the list (1 3 5 7 9).

    2. Write and test a function named getDecliningSequence that uses a for range loop to generate the numbers from n down to 1. For example, if n is 5, your function should generate the list (5 4 3 2 1).