A Gentle Introduction to Computer Algorithms

Joel P. Mugalu
Jul 11, 2020 · 5 min read
Person typing on laptop
Person typing on laptop
Image from Unsplash.com

Problem solving is a huge part of computer science. In fact computer programming is basically the process of solving a problem with code. These problems are solved by means of algorithms. An algorithm is a step by step instruction to solve a problem.

We implement algorithms in many aspects of our lives even without knowing it. For example if I wanted to make a cup of tea, I would take the following steps.

  1. Walk to the kitchen
  2. Find the electric kettle
  3. Grab the electric kettle
  4. Walk to the sink
  5. Fill the kettle with water to capacity
  6. Boil the water to 100 degrees
  7. Put tea into teapot and add hot water
  8. Pour tea from teapot into teacup
  9. Add 3 teaspoons of sugar
  10. Stir the sugar
  11. Drink Tea

What I’ve written above is what is commonly known as pseudo code. It’s a good practice to write pseudo code before writing an algorithm just to give you an idea of the steps you would take to solve the problem at hand.

Algorithms are built on fundamental concepts. These act like building blocks for writing algorithms successfully. We’ll go through these fundamental concepts for in this article.


Functions are like verbs or actions that tell a computer what to do. Functions are usually written for actions that would need to be repeated several times. The fundamental idea of functions is to wrap all the steps involved in carrying out a particular action in one block of code.

For example, we have several actions in our steps like walk boil grab fill etc. For such actions it would be ideal to write a function other than define them everywhere they need to be carried out.

When writing functions, it’s always good to keep the following in mind

  • A Function should do only one thing. Like walk boil or fill. A function should never carry out multiple actions
  • Its a good practice to try to keep functions as short as possible. A good rule of thumbs is the 10–10. 10 functions in a program and 10 lines in a function. Of course this is not a commandment but it’s something good to keep in mind.

I wrote an article about functions in JavaScript. Feel free to check it out here


Conditions are like branches that tell a computer which path to take in certain situations.

Conditions are written with the if keyword followed by the condition to be met. Multiple conditions can be nested together using the else if keyword. Its also possible to set a default outcome if none of the conditions are met using the else keyword.

Going back to our tea making algorithm, if we wanted to make sure that the water boils to 100 degrees, we could use a condition to check whether the water is 100 degrees then stop boiling.

if (water degree < 100 degrees) {
//continue boiling
} else if (water degree == 100 degrees || water degree > 100 degrees) {
//stop boiling
//water is ready

Notice that I used symbols such as < and ==. These are called operators.


A loop is a cycle that repeats something again and again. There are 3 basic types of loops.

While Loop

while (condition) {
// Execute this code

The while loop is a loop that repeats a cycle until a certain condition is met. At every iteration the loop checks whether the condition is true or false. If the condition evaluates to true the loop runs again. Else if the condition evaluates to false the loop stops iterating.

In our tea making algorithm we could use a while loop to implement step 5 to fill the kettle to capacity. Note that this is a repetitive activity because we are continuously filling the kettle with water. But that only stops when the kettle is a at it’s maximum water holding capacity.

Using a while loop the pseudo code for that step could be:

while (water level <= maximum kettle capacity) {
// Continue filling kettle with water

Do…While Loop

do {
//Carry out once
} while (condition)

The do…while loop is quite similar to the while loop. The only difference is that this loop must run at least once. Unlike the while loop which checks the condition before running, the do…while loop runs then checks the condition. Like the while loop, it stops running once the condition evaluates to false.

We can also use the do…while loop to implement step 5 of our tea making algorithm. This is because we by all means need some water in order to carry out step 6 which is boiling the water.
Using a do…while loop the pseudo code would be written:

do {
// add some water
} while (water level <= maximum kettle capacity)

For Loop

for (variable i; i < numberOfTimes; i++) {
//Code for each iteration

A for loop is another type of loop. A for loop works best when we want to repeat an action a specified number of times. Its body has three parts each separated by a semicolon.

In the first part a counter variable is initialized. This variable keeps track of the loop.
The condition goes in the second part of the loop. This part of the loop tells the loop when to stop. The loop checks this condition before iterating again.
The third part of the loop updates the counter variable.

In our tea making algorithm, we can use the for loop to implement step 9. This is because in this step we are adding sugar to the tea a total of 3 times.

for (variable i = 0; i < 3; i++) {
// Add sugar to the tea


Boolean are like questions that we ask a computer and we get a yes/no answer or more accurately true/false. We use Boolean to check for certain things.
In our tea making algorithm we could use Boolean to check whether we added enough sugar to the tea.

if (tea has enough sugar) {
return true;
return false;


The goal of this article was to give a gentle introduction to algorithms and provide a sort of mental model for how to solve algorithms and how to use the building blocks. As you might have noted the code in the examples is not language specific. I would advise you to do some research on how each of these building block is implemented in the programming language you’re familiar with. The syntax changes but the general idea remains. Feel free to reach out or connect with me on Twitter @codingknite

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Joel P. Mugalu

Written by

Front-End Developer. Connect with me on Twitter @codingknite

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Joel P. Mugalu

Written by

Front-End Developer. Connect with me on Twitter @codingknite

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store