A lot of enthusiasm is being shown towards programming and it has become a very important endeavor that a lot of people areconstantly joining it. There are a lot of programming languages out there and each one of them have their own characteristical features, but a very common element that pops us in a lot of them is a variable.
People who have been attending math classes before starting the learning process of a particular programming language might have come across the expression of this form:
a = a +1
and it can look really strange, because you know that it is mathematically wrong. How can a value be equal to its value plus one? Well, it is true that it is mathematically wrong, nonetheless it does make sense in the programming world and similar expressions are used very often.
How is it possible?
The equal sign in the above expression does not mean that the variable a is equal to a+1, but it is actually a kind of an assignment, where we are increasing the value of the variable a by one.
Let’s start to explain the intuition that you might want to have when you are facing similar expressions and cannot convince yourself about its meaning. Think of that variable as a kind of container, or a kind of jar, that contains a certain value, for example the value of five. The variable a should be seen as a container that has the value equal to five, and when we write:
a = a+1
first, we are increasing the previous value that the variable had and then we are assigning that value to the variable. The same reasoning applies to other cases as well, when we have other number values that we are trying to add to the value of a variable.
Variables have a very important role, and a proper understanding of their nature can be really helpful in your programming journey.