A Comprehensive Introduction To Computer Algorithms
Tags: codenewbie, beginners, webdev, computerscience
Published: 2020-07-07 11:33:50

Problem solving makes up 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

Perhaps I missed something or I took weird steps but that's not important. What I've written above is what is commonly known as pseudo code. Its good to write pseudo code before solving an algorithm just to give you an idea of the steps you would take to solve the algorithm.

The above steps are used an an example in this article. A human would take the above steps to make tea without a problem. But that is not the case with a computer. In order to write an algorithm similar to the above we would need to use building blocks for writing algorithms.

## 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. For example in our steps we have several actions like **walk** **boil** **grab** **fill** etc. For such it would be ideal to write a function other than define them everywhere they need to be used.
After writing the function we would simply call the function where it needs to be used.

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](https://dev.to/codingknite/javascript-functions-broken-down-4fgh)

##Conditions
Conditions are like branches that tell a computer what to do under certain conditions or at certain situations. Going back to our tea making algorithm, how would we know that the water has boiled to 100 degrees? In such a situation we could use a condition to check whether the water is 100 degrees and then stop boiling. Conditions start with the **if** keyword and they can be nested with the **else if** keyword to check for multiple conditions. Its also possible to set a default outcome if none of the conditions are met using the **else** keyword.

In pseudo code code, this would be written as: 
```
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 written:
```
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 it 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 certain 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
Using a for loop the pseudo code would be such as follows:
```
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. 
Using boolean this would be implemented In pseudo code such as:
```
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 your programming language. The syntax changes but the general idea remains. Feel free to reach out to me or connect on Twitter [@codingknite](https://twitter.com/codingknite)