What Is an Algorithm?
Imagine that you are explaining to a friend how to walk from your home to a park. If you say, “Go outside, move forward a little and turn over there,” your friend may not know which door to use, how far to walk or which direction to turn. The instructions may sound reasonable, but they are not precise enough.
Now describe the route like this:
- Leave through the main entrance of the building.
- Turn right.
- Walk straight to the first pedestrian crossing.
- Cross when the light turns green.
- Enter the park through the gate on the left.
The second description is a sequence of clear, executable steps. A clear and finite set of steps designed to achieve a goal is called an algorithm.
Algorithms are not limited to computers and robots. A recipe, the rules of a game, a school-bag checklist, the stages of an experiment and a training plan can all be examples of algorithms.
Short definition: An algorithm is a clear set of steps used to solve a problem or complete a task.
Four qualities of a good algorithm
1. It should be clear
“Wait for 10 seconds” is clearer than “wait for a while.” Computers and robots cannot interpret vague language the way people often do.
2. It should be in the right order
You need to put on a shoe before tying it. Correct steps in the wrong order can still produce the wrong result.
3. It should be finite
An algorithm should have a beginning and an ending. Instructions that continue forever are not useful unless an endless loop is the intended design.
4. It should be testable
We should be able to follow the steps and check whether they produce the expected result.
The three basic building blocks
Programming languages may look different, but most programs use three core ideas: sequence, condition and repetition.
1. Sequence
Commands run in a particular order.
Example: Moving a character on a screen.
Start
Move 2 steps right
Move 1 step up
Say “Hello”
End
Changing the order may move the character to a different position or display the message at a different time.
2. Condition
Some steps are carried out only when a particular situation is true.
Example:
If it is raining
take an umbrella
Otherwise
do not take an umbrella
Robotics example:
If the distance is less than 10 centimetres
stop the motors
turn on the red LED
The word “if” introduces a condition that lets a program make a decision.
3. Repetition
Instead of writing the same steps many times, we can repeat them inside a loop.
Repeat 4 times
move forward 1 step
turn right 90 degrees
A character following this algorithm draws a square. Changing the number of repetitions or the turning angle creates different shapes.
Everyday example: Checking a school bag
Goal: Make sure the required items are in the bag before leaving home.
In ordinary language
“I check my bag.”
This may sound clear to a person, but it is too vague for a program. It does not explain what to check, in which order or what to do when something is missing.
As an algorithm
Start
Open the timetable
Check the required book for each lesson
If a book is not in the bag
put the book in the bag
Check the pencil case
If the water bottle is empty
fill the water bottle
Close the bag
End
This example includes:
- A sequence of commands.
- A condition introduced by “if.”
- Repetition when checking each lesson.
What is pseudocode?
Before translating an algorithm into a real programming language, we can write it in an organised form that is close to ordinary language. This is called pseudocode.
Pseudocode is not meant to be run directly by a computer. Its purpose is to help people understand the solution clearly.
Example: Deciding whether a number is odd or even.
Start
Receive a number
If the remainder after dividing the number by 2 is 0
display “Even number”
Otherwise
display “Odd number”
End
The same idea in Python:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
The code looks different, but the underlying algorithm is the same.
How should we think about flowcharts?
A flowchart uses shapes and arrows to show the steps of an algorithm.
Basic symbols:
- Oval: Start or end.
- Rectangle: An action or process.
- Diamond: A yes/no decision.
- Arrow: The direction from one step to another.
Example: “Is it raining?”
[Start]
|
<Is it raining?>
/ \
Yes No
| |
[Take an [Continue]
umbrella]
\ /
[End]
Flowcharts become especially useful when an algorithm includes several conditions.
Mini challenge: Guide the robot to the target
A robot needs to move from its starting point to a red box. There are two empty squares in front of it, followed by an obstacle. The obstacle can be passed on the right.
You may use these commands:
- Move forward one square.
- Turn right.
- Turn left.
Task
Write the steps in order so that the robot reaches the target.
Example answer format:
1. Move forward one square
2. ...
Check your answer
- Did you use vague words such as “a little” or “over there”?
- Which direction is the robot facing after each command?
- Can you reach the same result with fewer steps?
There may be more than one correct route. What matters is that the algorithm is clear, safe and executable.
Debugging: What if the algorithm does not work?
Finding and correcting a mistake in an algorithm or program is called debugging.
Try this process:
- Write down the expected result.
- Observe what actually happens.
- Break the algorithm into smaller parts.
- Test one step at a time.
- Find the first point where the result becomes different from what you expected.
- Change only that step and test again.
Example:
Goal: Make the character draw a square
Problem: The character draws three sides and stops
Check: The repeat value is 3
Fix: Change the repeat value to 4
Making a mistake is not the same as failing. Understanding the mistake and improving the solution is a central part of programming and engineering.
Common mistakes
Giving vague instructions
Weak: “Move forward a little.” Better: “Move forward 20 centimetres.”
Skipping a step
Weak: Connect the LED and turn it on. Missing: The power source, resistor, polarity and safety check.
Ignoring the other result of a condition
We wrote, “If the door is open, go inside.” What should happen if the door is closed? A good algorithm considers the “otherwise” path when it matters.
Creating an endless loop
If a loop has no stopping condition, the program may never end. Some systems use an infinite loop on purpose, but even then there should be a safe way to stop the system.
Write your own algorithm
Choose one task:
- Prepare a glass of water.
- Check a bag before basketball practice.
- Complete a safety check before cycling.
- Prepare for a swimming session.
- Make a Scratch character collect a star and return to the starting point.
Try to include:
- At least five ordered steps.
- At least one condition.
- At least one repetition when appropriate.
- A clear beginning and ending.
Example: Cycling safety check
Start
Put on a helmet
Check the tyre pressure
If a tyre is too soft
ask an adult for help and inflate it to the correct pressure
Check the brakes
Check the lights and reflectors
If any part is unsafe
do not begin the ride
Choose a safe route
End
Lesson summary
- An algorithm is a clear set of steps for completing a task.
- Sequence, condition and repetition are fundamental structures.
- Pseudocode helps us describe a solution without depending on a particular programming language.
- A flowchart makes steps and decisions easier to see.
- Debugging compares the expected result with the actual result to locate a problem.
- A good algorithm is clear, correctly ordered, finite and testable.
Check questions
- Which two basic qualities must a sequence of steps have before it can be called an algorithm?
- How would you explain the difference between sequence, condition and loop in one sentence each?
- What advantage does pseudocode offer over writing immediately in a programming language?
- Why should “the water bottle is empty” be treated as a boundary case in the school-bag algorithm?
- When an algorithm produces the wrong result, what evidence should you collect before changing it?
Answers
- The steps must be clear enough to follow, and the process must finish after a finite number of steps.
- Sequence defines order, a condition selects a decision for a situation, and a loop repeats one or more steps.
- Pseudocode makes the reasoning visible without forcing the writer to deal with the syntax of one language.
- An algorithm should define its behaviour not only for the normal case but also for missing or unexpected inputs.
- Record the input, expected output and actual output, then trace the steps until you find the first point where they differ.
Source and verification note
For “What Is an Algorithm?”, verification focuses on whether the relationship between What Is an Algorithm? and 1. It should be clear remains consistent across examples. The algorithms in this lesson are checked by tracing sample inputs by hand and comparing them with expected outputs. Pseudocode is used to make the reasoning sequence visible without tying it to one programming language.
End-of-lesson check
- How would you define What Is an Algorithm? in your own words?
- What is one normal use of the structure learned in this lesson?
- Which boundary or unexpected case would you test?
- How could you detect and correct one likely mistake?
- How would you adapt the same idea to another robotics or coding project?
End-of-lesson check — sample answers
- A good definition explains both the main idea and its purpose.
- The example should identify the input, the process and the resulting output.
- A boundary test can use the lowest or highest accepted value; an unexpected test can use missing or invalid input.
- Compare expected and actual results, change one thing at a time and repeat the test.
- Find the rule that remains the same, then adapt the steps to the new project’s input, tool and output.
Next lesson
Sequence, Conditions and Loops in Scratch: Build a first interactive program that guides a character through a maze.
Quiz call to action
When you are ready, test your understanding with the 10-question Robotics & Coding Starter Quiz. Read the short explanation after every answer to see what you already understand and what you may want to review.