📋 Algorithms: Instructions for a Computer

2nd Grade 🤖 Artificial Intelligence

An algorithm is an ordered list of steps that leads to solving a task. A cake recipe, the instructions for putting a toy together and the route you take to school are all algorithms. Computers — and AI too — run on algorithms.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

What is an algorithm?

An algorithm is a clear set of instructions: you do the first step, then the second step, and so on until the task is done. Every one of us uses algorithms every day without calling them that. When you brush your teeth — wet the brush, add toothpaste, brush, rinse — you’re following an algorithm.

The word “algorithm” comes from the name of a Persian mathematician, Muhammad al-Khwarizmi, who lived more than a thousand years ago and worked in Baghdad. He wrote books about step-by-step ways to solve math problems, and over time his name turned into a word for any orderly method of solving a problem.

A computer needs very exact instructions

A person can understand an instruction like “make a sandwich” without any more explanation. A computer can’t. It does exactly what’s written — no more and no less. Imagine a robot getting the instruction “spread peanut butter on the bread.” Should it open the jar first? Which hand holds the knife? Which side of the bread gets the peanut butter? If we forgot to write “open the jar,” the robot will try to spread with the jar still closed.

That’s why programmers write very detailed, carefully ordered instructions in a special language the computer understands, called a programming language. When there’s a tiny mistake in the instructions — say, a step in the wrong order — the result can be funny or completely wrong. A mistake like that in a program is called a “bug.”

Conditions and loops

Algorithms have two important tools. The first is a condition: “If it’s raining, take an umbrella; otherwise, take a hat.” A condition lets the algorithm choose what to do depending on the situation. A spam filter, for example, checks conditions: if a message has suspicious signs, it goes to the spam folder.

The second tool is a loop, which means repeating: “While the glass isn’t full, keep pouring.” Instead of writing the same instruction a hundred times, you write it once and tell the computer to repeat it. A maps app, for example, checks over and over where you are and updates the route.

What does this have to do with AI?

Every program, even the simplest one, is built from algorithms. So is AI. The difference is that AI uses a special kind of algorithm — a learning algorithm. Instead of a programmer writing a rule for every situation, they write an algorithm that can learn from examples: look at lots of cases, check where it went wrong, and improve itself a little each time.

Here’s one way to think about it: a regular algorithm is like a fixed recipe. A learning algorithm is like a cook who tries, tastes, adjusts the amounts and gets better over time. But even that cook follows instructions people wrote, and people are responsible for checking that it works well.

Activity: program your parent

Here’s a funny game to play at home: write instructions for making a sandwich on a sheet of paper, then ask a parent or a brother or sister to follow them exactly as written — no guessing and no filling in missing steps. You’ll quickly discover how many steps you forgot. Then fix the instructions and try again. That’s exactly what programmers do: write, test, find bugs and fix them.

Another challenge: write an algorithm for getting from your classroom to the school office, with at least one condition — like “if the door is closed, knock” — and one loop, like “keep walking until you reach the corner.” Have a friend follow the instructions with eyes open and carefully. If they reach the goal without asking any questions, you wrote a good algorithm.

Key facts

  • An algorithm is an ordered series of steps for solving a task.
  • The word “algorithm” comes from the name of the mathematician al-Khwarizmi.
  • A computer carries out instructions exactly as they’re written, without guessing what you meant.
  • Conditions (if… otherwise) and loops (repeating) are basic tools in algorithms.
  • AI uses algorithms that can learn from examples.

Quick quiz

  1. What is an algorithm?

  2. Which of these is an algorithm from everyday life?

  3. What happens if we forget a step in the instructions for a computer?

  4. What is a “bug”?

  5. Which sentence is an example of a condition?

  6. What does a loop do in an algorithm?

  7. Where does the word “algorithm” come from?

  8. What’s special about an AI algorithm?

💭 Think and talk about it

  1. Write a five-step algorithm for leaving the house in the morning. What condition would you add?
  2. Why do you think it’s harder to write instructions for a computer than to explain something to a friend?
  3. Could you write an algorithm for “how to be a good friend”? What would be easy about it, and what would be hard?

Questions kids ask

Does an algorithm have to be on a computer?

No. Any ordered list of steps is an algorithm — a recipe, assembly instructions, or the way you solve a long division problem.

What’s the difference between an algorithm and a program?

An algorithm is the idea — the order of the steps. A program is the algorithm written in a programming language so a computer can carry it out.

How do I start learning to code?

You can start with kids’ coding languages where you drag and snap together blocks of instructions, and with games where you guide a character using steps, conditions and loops.

🔎 Ask Thinkapedia another question