# A Gentle Introduction to Computer Algorithms

Jul 11, 2020 · 5 min read

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

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

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.

# Loops

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

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;`

# Conclusion

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.

### 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.

Medium sent you an email at to complete your subscription.

Written by

## Joel P. Mugalu

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.

Written by

## Joel P. Mugalu

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.