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.

#fundamentals #algorithms #programming #beginner #learning-to-code #algorithms

A Gentle Introduction to Computer Algorithms
1.40 GEEK