An algorithm is a step-by-step process designed to achieve some outcome. Computers are the greatest machines ever conceived for doing step-by-step processes.
As a result, the study of algorithms is very closely tied to computers, computer science, and programming.
Algorithms are important to computer science, but they aren't exclusively part of computer science. You’d need to think about algorithms if you went out of town and needed to get a pet sitter for your beloved hamsters.
Imagine your pet sitter is very good at following directions, but they’re not very good at using their own judgment, so you need to give very detailed feeding instructions.
Check the hamster’s food bowl.
If the food bowl is empty, put five pellets in the bowl.
Otherwise, if the food bowl does have some leftover food pellets in it, put three pellets in the bowl.
There are four pellets in the bowl after your sitter runs this algorithm and before the hamsters have a chance to nibble on anything. What can you conclude?
Normal English sentences may suffice when you want to give specific instructions to your hamster sitter. Most algorithms that are designed to be run on computers are much, much longer and more complicated than your instructions for the sitter. Describing these algorithms in paragraph form would get ambiguous, confusing, and extremely verbose.
This problem is not new to computing! Recipes and board game rules are two other examples of complex rules for performing some task (making dinner or playing a game, respectively).
Algorithms for making dinner and playing board games are often described in text, just like your hamster sitter instructions. This can lead to ambiguity and confusion, which you know if you’ve ever ruined dinner or fought over the rules of a board game.
When humans work with complex processes — especially repetitive processes — they tend to develop specialized ways of communicating. Baseball, knitting, and chess are all examples. In the case of chess, this information
"in the chess game's second move, black's king-side knight moves forward in front of the bishop"
is all described compactly by chess aficionados by writing Nf6.
This chess notation looks almost meaningless to an outsider, but it's still a language designed for humans to communicate. (It's not a computer code, and it's not used in the internal implementation of chess-playing computer programs.)
Pseudocode is the name for the specialized way that computer scientists express algorithms. Fundamentally, pseudocode is just a list of instructions. Here’s some pseudocode to help your hamster sitter get into your apartment:
Can you drag the blocks below to create an algorithm in pseudocode that helps your hamster sitter start their car after they leave their house?
This is an example of a command or instruction. These two are also examples of commands:
Algorithms in computer science are almost never just sequences of commands. Algorithms can also include conditional instructions. Here’s an algorithm in English that is conditional on whether the hamster is hiding:
If the hamster is hiding, put on some soothing music. Otherwise, put her ball in the cage and play with her.
Here’s the same algorithm in pseudocode:
The line starting with
if describes the conditional instruction's test:
hamster is hiding.
The algorithm above uses a conditional instruction, a pattern you'll see constantly in computer science, algorithms, and programming:
If you want to make your pseudocode look more like written English, you could write
otherwise: instead of
else:. Nearly every programming language uses the words
else for describing the pattern of describing a test, commands for if the test succeeds, and alternative commands for if the test fails. This course uses
else in order to look more like those programming languages.
These even more detailed instructions for your hamster sitter include two conditional tests.
Check the hamster’s food bowl.
If the food bowl has no food pellets left in it, do two things: put two fruit pellets in the bowl, and then put three veggie pellets in the bowl. Otherwise, if the food bowl does have some leftover food pellets in it, just put three veggie pellets in the bowl.
Finally, the hamsters are less active when it rains. If rain is not forecast on tonight's weather report, add one more veggie pellet to the bowl.
Arrange the following lines to recreate this algorithm in pseudocode. You can drag instructions into the inside of conditional instructions (instructions that start with
Conditionals can be nested inside of each other to form complex decision-making algorithms.
Consider the scared hamster from before.
He will decide to come out and play if there's soothing music playing. He will also decide to come out and play if there's neither rain noises nor noises of barking dogs in the neighborhood.
By dragging and dropping commands and conditional instructions inside of one another, you can build an algorithm that replicates the hamster's decision-making. Remember, the instructions underneath an
else run whenever the test after
You've now seen and used commands and conditional instructions to describe a complex algorithm intended for a human.
Commands and conditional instructions are two of the fundamental pieces of both pseudocode and programming languages.
All alone, commands and conditionals don't let you describe much more than pet sitter algorithms. The rest of the quizzes in this introduction will explore other key algorithmic ideas and the increasingly interesting algorithms that can be described with these extra pieces.