Algorithms, Logical Flow and Variables

So you want to become a Software Engineer, let’s talk about Algorithms. According to Wikipedia: “an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation” But who cares about that definition. In easy to understand language for us mortals an algorithm is a sequence of logical steps to solve a problem. Much easier to grasp right? Well, because it is. Here in Code Journey, we like to make complicated things easy to understand because everyone (that wants to) can learn to Code. Let’s dive into some more theory and examples of algorithms.

The 4 basic blocks of Algorithms

The basic blocks of algorithms are 4: Inputs, Outputs, Instructions, and Decision-making blocks (or IFs). There are others, you can add to the list call to functions, procedures, and so on, but for the purpose of understanding algorithms and logical flow, those 4 are the only blocks that matter.

Before we move to what those 4 blocks mean and examples we need to take something into account. When we draw algorithms or code them we have to think it from the perspective of the system, the computer. What does that mean Matt? Well, basically that when we say Inputs those are inputs to the system, that in our basic scenarios will be provided from the users (us). For example, when you open a Calculator and you say 4 + 5 those are inputs to the system, you entered a number, then an operand and then a number again. From the computer perspective, those are its inputs, and the output will be the result which will be 9 assuming the calculator works.

So now we have an idea of what inputs and outputs are. The other 2 blocks are instructions that can be declarations, assignations or operations and the famous IFs or decision making blocks. In order to advance further we need to talk a little about VARIABLES.

A variable is like a box, it’s a place where you can put things, in systems engineering those things are values. You can also imagine it as a tupper, you can put very different things in a tupper like pasta, some meat, carrots or milk (who puts milk in tuppers???? I don´t know) but there is also different type of tuppers for different things or amounts of a given thing.

Well, the same concept applies here. Variables have types that determine what can go inside of them and what cannot. (This does not apply to loosely typed languages, but we are not going to talk about that for now). Ending our analogy of boxes and tuppers lets put some examples. A variable can be of type: string (for a sequence of characters), int (for integer numbers), or bool (for boolean values, only True or False). There are MUCH more types, but we can do a lot with only those 3 for now.

So far we know variables can contain values and have a certain type. The only thing missing to know about variables is that they have a NAME. We need a way to access those values and that’s via its name. So a variable has a name, a value, and a type. So now that we know what variables are we can continue with our algorithm´s blocks.

I promise you we are almost there, you are learning all the basics about Algorithms and everything you need to create programs. 💪 

The last 2 blocks were instructions and IFs. For instructions we can do declaration: that’s when we give a name to a variable, we can do assignation that is when we give values to variables and we can do operations that’s when we use operands to create new values or results. Let’s see some code examples:

string name = "Matt"
int day = 20
int result = 2 + 8

First things first, the Equal operator (=) is for assignation (in most languages). That means that whatever is on the right side will be put on what is on the left side. This is very important, take some time to re-read the examples and remember this. If you understood that now you must be thinking that on the right side we will have values and on the left side we will have variables (containers). In our examples, we are creating variables by giving them types and names and assigning them values like a sequence of characters given by the double quotes (“) or numbers or even results of operation (the addition of 2 and 8). That is part of the algorithm, we are creating and manipulating variables.

This is an example of an algorithm on paper. Depending on the book or teacher you can find differences on the drawings but the ideas are the same. We have the input block, the process and the output.

I don´t want to spend much time on explaining the drawings because we are going to transition into coding very soon but I think its important to be a little familiar with this and understand the concepts before rushing into writing code.

And finally, the last but very important block is the IF. The if is a conditional statement. Basically it will evaluate a condition and then decide if the algorithm continues one way or the other. You can imagine it as a street that ends and you can turn right to go and buy a donut or turn left to go directly home. If you really want to eat that donut and you have the cash to buy it you will turn right, if not you will turn left. Thats exactly how an IF statement works. And this is the moment where you brain will explode… Our life is a never ending IFs statements based on Inputs. When you get up in the morning you get your inputs and make decisions, which clothes to wear depending on the temperature and the things you have to do that day, what to have for breakfast depending on how you feel and whats in your fridge and so on for the rest of the day. Our life is based on making decisions and the way we model that in programming is trhough IFs.

In the next post we will see some examples of basic code using these 4 blocks and some exercises for you to practice! 💻