Algorithms and Sequences
An algorithm is a step-by-step plan. Learn why order matters, guide a robot across a grid, and spot steps that are in the wrong place.
Welcome back!
Step 1 of 12Learn
Plans with steps
Before coders write code, they make a plan. A step-by-step plan for doing a task is called an algorithm.
You use algorithms every day. Brushing your teeth is one: get the toothbrush, put on toothpaste, brush, rinse. A recipe is an algorithm. So are the directions to your friend's house.
A good algorithm has steps that are clear, in the right order, and has a clear end.
Step 2 of 12Practice · Diagram
An algorithm for brushing teeth
Which step comes right after adding toothpaste?
Tap the shape that answers the question, then press Check.
- Get brush
- Add toothpaste
- Brush
- Rinse
- Done!
Step 3 of 12Practice · Put in order
Put this algorithm for planting a seed in order.
Drag the items, or use the arrow buttons, to put them in order.
- Drop in the seed
- Water it
- Poke a small hole
- Cover it with soil
- Fill a pot with soil
Step 4 of 12Learn
Order matters
Steps that run one after another are called a sequence. Computers always run code from top to bottom, one line at a time.
If the order is wrong, the result is wrong. Try putting on your shoes and then your socks. Silly!
Coders often test an algorithm by acting it out or tracing it with a finger, step by step, before they type any code.
Step 5 of 12Compare
Order matters!
| Aspect | Right order | Wrong order |
|---|---|---|
| Step 1 | Put on socks | Put on shoes |
| Step 2 | Put on shoes | Put on socks |
| Result | Ready to go! | Socks on top of shoes. Silly! |
Step 6 of 12Practice · Spot the mistake
One step of this algorithm for making toast is in the wrong place. Tap it.
Step 7 of 12Tap to reveal
Guiding a robot on a grid
The commands
Our robot knows three commands: forward (move one square), turn left and turn right (spin on the spot without moving).
The goal
The robot starts facing right. A star is 2 squares to the right and then 1 square up.
The algorithm
forward, forward, turn left, forward. Two moves right, a quarter turn to face up, then one move up.
Step 8 of 12Practice · Put in order
The robot starts facing right. Put the commands in order so it goes 1 square right, then 2 squares up.
Drag the items, or use the arrow buttons, to put them in order.
- turn left
- move forward 2 squares
- move forward 1 square
Step 9 of 12Note
Step 10 of 12Practice · Choose one
Which is the best algorithm step for a robot?
Step 11 of 12Practice · What would you do?
What would you do?
You wrote directions for a classmate to draw a house: "Draw a roof. Draw a square under it." They drew a tiny roof in one corner and a huge square across the page. What is the best fix?
Step 12 of 12Practice · Fill in the blanks
Fill in the blanks
A step-by-step plan for solving a problem is called an .
Lesson complete!