In Lesson 1.2 we introduced the term algorithm and defined it as a step-by-step procedure of precise instructions that performs some calculation or computation.
Algorithms are at the heart of computer science. Algorithms, expressed in computer code and interpreted by the computer, are what make our computers such powerful and adaptable machines.
As we saw in the maze problems in Lesson 1.1, algorithms are constructed out of basic building blocks called control structures. There are three basic control structures:
- Sequence– a sequence of instructions or statements.
- Selection– a conditional instruction that lets the program branch between two or more alternatives.
- Repetition (or Iteration)– a structure that repeats one or more instructions.
An amazing fact that has been proved by computer scientists is that all algorithms can be constructed by using just these three control structures. In other words, any algorithm that you would like to write to solve a problem can be built by a combination of sequence, selection, and repetition.
Make sure to view the PowerPoint about Algorithms in the Assignments pane, and complete your Portfolio items.
Writing Pseudocode: Critical Thinking Questions
Pseudocode is a cross between computer code and everyday English. There are many different varieties. Pseudocode is less precise than actual computer code, such as Java or Python or App Inventor, but more precise and less wordy than everyday English.
Suppose we have a list of numbers -- e.g., 5, 10, -2, -3, 7, 8, 12 Here's an algorithm that uses sequence, selection, and iteration (repetition) to add all the even numbers in the list and print out their sum. Notice how indentation is used to clarifiy the different parts of the algorithm.
- Set the running total to 0.
- For each number in the list:
-  If the number is even
-   Add the number to the running total.
- Print the running total.
This algorithm contains five lines. They are numbered for convenience.
- Which line(s) of the algorithm contain a repetition statement?
- Which line(s) of the algorithm contain a selection statement?
- If you ran this algorithm on the above list of numbers, what would it print?
- (Portfolio) Suppose you had a list of positive numbers such as 5, 10, 12, 13, 6, 7, 1, 3, 2, 1. And suppose for each of the numbers in the list you added the number to a running total if it is even and subtracted it if it is odd. What result would you get for this list of numbers?
- (Portfolio) Write a pseudocode algorithm that implements the algorithm you used to calculate this total.