If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: AP®︎/College Computer Science Principles>Unit 4

Lesson 1: Building algorithms

# The building blocks of algorithms

An algorithm is a step by step process that describes how to solve a problem in a way that always gives a correct answer. When there are multiple algorithms for a particular problem (and there often are!), the best algorithm is typically the one that solves it the fastest.
As computer programmers, we are constantly using algorithms, whether it's an existing algorithm for a common problem, like sorting an array, or if it's a completely new algorithm unique to our program. By understanding algorithms, we can make better decisions about which existing algorithms to use and learn how to make new algorithms that are correct and efficient.
An algorithm is made up of three basic building blocks: sequencing, selection, and iteration.
Sequencing: An algorithm is a step-by-step process, and the order of those steps are crucial to ensuring the correctness of an algorithm.
Here's an algorithm for translating a word into Pig Latin, like from "pig" to "ig-pay":
``````1. Append "-".
2. Append first letter
3. Append "ay"
4. Remove first letter``````
🔍 Try following those steps in different orders and see what comes out. Not the same, is it?
Selection: Algorithms can use selection to determine a different set of steps to execute based on a Boolean expression.
Here's an improved algorithm for Pig Latin that handles words that starts with vowels, so that "eggs" becomes "eggs-yay" instead of the unpronounceable "ggs-eay":
``````1. Append "-"
2. Store first letter
3. If first letter is vowel:
a. Append "yay"
4. Otherwise:
a. Append first letter
b. Append "ay"
c. Remove first letter``````
Iteration: Algorithms often use repetition to execute steps a certain number of times or until a certain condition is met.
We can add iteration to the previous algorithm to translate a complete phrase, so that "peanut butter and jelly" becomes "eanut-pay utter-bay and-yay elly-jay":
``````1. Store list of words
2. For each word in words:
a. Append hyphen
b. If first letter is vowel:
i. Append "yay"
c. Otherwise:
i. Append first letter
ii. Append "ay"
iii. Remove first letter``````
By combining sequencing, selection, and iteration, we've successfully come up with an algorithm for Pig Latin translation.
🤔 Can you think of situations where it produces an incorrect output?

## Want to join the conversation?

• I had to look up pig Latin in dictionary, may be a more internationally understood concept could be used as example for better understanding.
• You are really right! Why make things weirder?
• This is very confusing to me!! I do not understand the difference between the three (iteration, sequencing, and selection) can you help me?
• Are you familiar with programming? A programming background helps to understand, since they map to programming concepts. Sequencing is a part of all programs, as it's just the fact that computers execute programs in a particular sequence (like top to bottom line in a simple program). Iteration is when we use loops to repeat code in a program. Selection is when we use conditionals (if/else) to execute different blocks of code in a program.
• Would something go wrong if the list of words is initially empty?
• From the author:From the way I read it, an empty list is compatible with the pseudocode. It would get to step 2, see that there are no words in the list, and not execute any of the operations under step 2.
That's a great case to consider when you're turning this pseudocode into real code, however! I have very often had bugs in code arise from my failure to consider the case of an empty list.
• There are three building blocks of algorithms: sequencing, selection, and iteration. Sequencing is the sequential execution of operations, selection is the decision to execute one operation versus another operation (like a fork in the road), and iteration is repeating the same operations a certain number of times or until something is true. Which of those building blocks is confusing to you?
• What's the difference between an algorithm and a program?
(1 vote)
• Hi Chawin!

An algorithm is a way to condense code in such a way that you are able to use that algorithm to have a more efficient program. A program is the general code that creates an output.
• Iyay eakspay igpay atinlay
• I somewhat get it. The pig Latin part does confuse me though.