In this lecture we will talk about algorithms. We'll look at many different examples. We'll learn about the types of components (steps) that make up algorithms, including sequence, selection, and repetition, and we'll learn how to develop and express some simple algorithms.
In computer science or mathematics an algorithm is a step-by-step method for performing a calculation. Each step in the algorithm must be unambiguous and doable. And there should be a finite number of steps.
We mean calculation in the broad sense that anything a computer does is a calculation. For example, changing the background color of a button when the user clicks on it is a calculation.
An algorithm is similar to recipe directions. But for algorithms we want to run on a computer the steps in a recipe are sometimes ambiguous.
5. Slowly blend in the flour mixture and mix until just combined. Do not overbeat.
An algorithm is similar to hair washing instructions. But some of its steps are ambiguous
Repeat application and rinse if hair is oily, or has excessive build-up.
Write the word 'sum' on the board and put a 0 underneath it. Add 5 to the number written under 'sum' and put the result under 'sum'. Add 10 to the number written under 'sum' and put the result under 'sum'. Add 13 to the number written under 'sum' and put the result under 'sum'. The number written under 'sum' is the sum of 5, 10, and 13.
In this example, the word 'sum' is like a variable. If we were writing this algorithm for App Inventor it might look like this, where Sum is a global variable.
Set Sum to 0. Set Sum to Sum + 5 Set Sum to Sum + 10 Set Sum to Sum + 15
Here's an App Inventor Procedure that implements this algorithm:
Of course this algorithm is somewhat artificial. It doesn't really
need 4 steps because it can be expressed more simply as:
Questions:
Suppose we represent a list of numbers by using parentheses: (5 10 13). Here's an algorithm for calculating the sum of this list of 3 numbers.
Set Sum to 0. Set Sum to Sum + the first number in the list. Set Sum to Sum + the second number in the list. Set Sum to Sum + the third number in the list.
When we say the first number of the list we are indexing the list. This is a common step in computer algorithms.
Here's an App Inventor Procedure that implements this algorithm. Note that the
select list item block takes an argument for the list's index:
Abstraction: Note how the argument slots in the select list item block generalize the operation of selecting an item from a list.
Suppose we wanted to add just the two-digit numbers in our list of three numbers. We could use this algorithm:
Set Sum to 0. If the first number in the list >= 10 Set Sum to Sum + the first number in the list. If the second number in the list >= 10 Set Sum to Sum + the second number in the list. If the third number in the list >= 10 Set Sum to Sum + the third number in the list.Or we could use this algorithm that uses a second global variable, called NumberSet Sum to 0. Set Number to the first number in the list. If Number >= 10 Set Sum to Sum + Number. Set Number to the second number in the list. If Number >= 10 Set Sum to Sum + Number. Set Number to the third number in the list. If Number >= 10 Set Sum to Sum + Number.
Here's two App Inventor way to express these two algorithms:
![]() |
![]() |
Questions:
Suppose your list could have a variable number of numbers in it. Here's an algorithm for that.
Here's how we would do this in App InventorSet Sum to 0. Set Index to 1. While Index <= the length of the list Set Sum to Sum + the list item at Index Set Index to Index + 1
Statement Type | Example |
---|---|
Assignment: | Set Sum to 0 |
Assignment: | Set Sum to Sum + 10 |
If/Then: | If Number > 10 then-do Set Sum to Sum + 10 |
Loop: | While Index <= Length of List do: Set Index to Index + 1 |
Procedure call: | Call SomeProcedure |
Sequence | Selection | Repetition |
---|---|---|
Set Sum to 0. Set Sum to Sum + 5 Set Sum to Sum + 10 Set Sum to Sum + 15 |
Set Sum to 0. If the first number in the list >= 10 Set Sum to Sum + the first number in the list. If the second number in the list >= 10 Set Sum to Sum + the second number in the list. If the third number in the list >= 10 Set Sum to Sum + the third number in the list. |
Set Sum to 0. Set Index to 1. While Index <= the length of the list Set Sum to Sum + the list item at Index Set Index to Index + 1 |