As mentioned in a previous post, programs are lists of instructions that indicate to devices how to process some input data to produce the desired output.
To do so; programs must keep a registry of those input values, and of every other resulting data, this storage of information is made on the machine’s memory through some logical structures called Variables.
Using a cooking analogy, let’s suppose we were hired by a restaurant as chefs and we must cook an omelette, the ingredients of it would be:
To start our cooking in an ordered fashion, we decide to store all the ingredients in some containers and add a label to each; that way, when we would have to use each ingredient in our process, we would know exactly where to find it.
A variable is a “memory container” where we can store data that will be used in our programs, to keep a registry. Those “containers” are labeled with a name that we assign in the moment of their creation and we can use to make reference to them.
Variables may contain very different sorts of data: numeric, alphanumeric, boolean, dates, etc.
To show how variables are used in algorithms, let’s see the following example:
In this example, x is the variable in which we initially store a value of 20, then, in a second statement, we increment its value by 1, becoming now 21. Finally, the algorithm prints the current value of x.
In other words, we created a variable (called x) and stored some value on it, performed some operations over its value (increment by 1) and finally used its name to have access to its current value (print x’s value).
Besides allowing us to store data in memory, variables can be combined with others to generate new values or modify their data, that combination can be done through the use of operators.
For a better explanation, let’s back to the cook to prepare the dish of the day: Omelette. To make it, we have been instructed that we must:
- Spill Oil in a pan
- Mix up eggs with ham in the same pan
- Add salt and pepper
Some of these steps (Spill, Mix, Add) involve activities with the ingredients we already have stored. In the same way, the development of a program may include actions (operations) with variables data.
Operations can be of many kinds, depending on the type of data that can be used with them. For example, when using numeric data we can use arithmetic operators (addition, subtraction, multiplication, division, remainder) to combine values and produce new ones; for alphanumeric values we can produce new results by concatenating variables or by spliting the content of them.
The following is an example of the use of operations in an algorithm:
In the previous algorithm, we created two initial variables (x and y), set their values to 20 and 30 respectively, and later combine them using subtracion to produce a new value that we store in a variable called z, to finally print this value.
- Variables are “memory containers” in which we can store data to be used in our programs
- Variables are labeled and we can access their data using the assigned label
- Operators allow us to combine data contained within variables