Statements and Variables
There may be a lot of terms used in these articles that you might not be familiar with, and the best effort will be made to explain those terms as they are used. If you're curious where they come from, however, most of them have roots in mathematics, as that is mostly what programming is, mathematics. These terms are not used to confuse but are terms that are used by people in the field heavily to communicate, and it can be greatly beneficial to start to familiarize yourself with them.
Starting with our first bit of terminology, the word statement will be used in various parts of the ensuing text, and it's important to understand what this means when it is used. A statement in programming is basically a single instruction that we are giving the machine. If you imagine telling someone how to draw a plus (+) sign, first you might say "Draw a vertical line". This is a statement, a single instruction that you are giving them to do something. Next you might say, "Draw a horizontal line through the middle of the vertical line". This is another statement, and with these two statements together, you have given instructions on how to draw a plus sign.
This is the exact same concept of statements in programming, with the only big difference being that statements (instructions) in programming tend to be much shorter and much simpler (and we use a lot more of them!). The machine understands only very basic instructions, and you need to be much more specific about how to do things. A single instruction that you might give someone else verbally could end up being multiple (even dozens or hundreds of!) very tiny statements when written in a piece of code.
Variables are what we use to reference a piece of data that can have many different values. If you're familiar with basic algebra, equations are often given with certain values replaced with letters.
y = 3x + 4
These letters are variables, they are placeholders for some value that maybe we don't necessarily know right now, but it allows us to reference them using the letter (or label) that represents them. Variables in programming serve the same purpose, they allow us to reference some value, essentially storing it inside the variable.
A variable could be thought of like a box with some name or other designation written on the side. If you were to move to a new house, you would mark the outside of the boxes with labels like "kitchen" or "towels". You can put different items in that box, the same as how we can give variables different values in programming. Lets look at an example.
let x; x = 5;
This example has two statements (remember what statements are from above?) with several pieces to it, starting with the first group of letters in the first statement, 'let'. This word identifies that we are declaring a new variable that did not exist before. If you think of our moving scenario, it would be like taking a fresh unfolded box and assembling it, taping the bottom, thus making it ready to put items in, and yet it doesn't have anything in it yet. We only need this the first time we reference the variable and never again during it's life (once the box is made you don't need to make it again!).
The next group of letters (or single letter in this case) after that is the x. The 'x' is the name we have given our variable, and it could be anything, such as 'y' or 'person' or 'address'. The name of the variable isn't important as far as the machine cares, it is simply there to remind us humans which variable it is, since it is likely we will have other variables (possibly lots!). Thinking again of the moving scenario, we had marked our boxes with 'kitchen' and 'towels' so that we can tell one box (variable) from another, which will be helpful when we go to do something with them later.
Note: Not all languages require to you declare a variable, some will just allow you to start using a new variable right away, like PHP or Python. For these languages, we could skip this first statement entirely, and for languages where we do have to declare our variable, the way you do it can be different for each language.
While the name of the variable doesn't affect how the machine runs our code, it is important to give good meaningful names to our variables so that we can remember what they are for. If you can imagine moving to a new house and your boxes were named 'a', 'b', 'c', etc., you can understand how that might make life difficult later when trying to find certain things or figuring out which boxes go where. Even the label 'kitchen' is somewhat less meaningful than it could be, as 'kitchen' could be anything from silverware, dishes, cereal, appliances, or anything that might go in a kitchen. On the other hand, the box labeled 'towels' leaves very little room for interpretation, which makes for the best type of label. This is the same for variables in programming, and it becomes apparent how important this is later when making more complex programs with lots of variables. You can read more about variable naming in <link: Variable Naming>.
Following the declaration 'let', and the variable name 'x', we have an interesting symbol ';' (semi-colon) at the end of our statement. This is to denote the end of our statement, and it is not always required in every language, or even used at all, but is still frequently used in many others. You can think of the semi-colon the same as if we were writing a sentence and we end that sentence in a period. With this, our first statement is complete, and with it we have said "I have a new variable, and it's name is 'x'".
On the next line we start our second statement. Now that we have created a variable, we want to do something with it. First we specify which variable we are going to use (as we may have made several others before this), which is 'x' in this case. Following that, we have what we refer to in programming as an "operator", which is the = (equals) sign. There are many different operators that we use, and the equals sign is just one of them. We should all be familiar with this symbol from math classes, as it is how we state what the value of something is. This concept is very similar to how we use it in programming, as it is used to assign a value to our variable, and as such, it is referred to as the "assignment operator". Again with the moving scenario, this is where we actually put items into the box, the lid is open and we are about to stuff something in, but what are we stuffing in? That's where we come to the final part of our second statement, the 5. This is what value we are assigning to our variable, or rather, what we are stuffing into our box. Again we end our 'statement' with a semi-colon to denote it's end, and we now have a second statement that reads "My variable named 'x' has a value of 5".
Together, we have made two statements, the first of which is "I have a new variable, and it's name is 'x'", and the next statement being, "My variable named 'x' has a value of 5". For the purposes of the prior explanations, the code had been broken into two statements, however we could have also just as easily written it as a single statement, both declaring the variable and initializing a value at the same time.
let x = 5;
Both methods are valid uses, and which one you use depends on the code you need to write (more on this in other articles). Also, note the term that was just used, initializing. This is a term often used in programming to denote that we have assigned a new value to a variable. Also take note that you can initialize a variable many times, but you can only declare (reference this term from earlier in the article) it once for a given scope (read more about Variable Scope. If you were to do the following code:
let x = 15; x = 10;
In the first statement, we created a new variable called 'x' and initialized it (assigned it's value) to 15. In the second statement, we then re-initialized it (reassigned it's value) to 10. The previous value that used to be in x, 15, is now gone, as if it didn't exist, and x is now 10. In essence, every time you use the assignment operator (=), you emptying the variable and putting something new inside.
One last thing that we can do is instead of using a numeric literal (a literal is when we reference a value directly, such as writing the actual number 5, this will be made more clear in a bit), is we can assign the value by using another variable. So say we have:
let x = 20; let y = x;
In this code, what we have done is assign the value 20 to x using a numeric literal, and then we have assigned whatever is in x to y as well. You can think of this as with our boxes scenario, when we do let y = x, we are saying, make a copy of whatever is inside box 'x', and put it in box 'y'. So now they both have the same value in them. Again, while the first statement used the numeric literal '20' to assign a value, the second statement did not use a literal, instead it referenced the value of another variable.
Note: Assigning values from one variable to another will not always make a "copy" as we explained in this section, however when we are dealing with simple values like numbers or strings, this is most likely the behavior. More details on this in <link: Reference vs Value>
Congratulations, you now know how to create a variable and give it a value! Continue on to read about Program Output in the next segment of the Introduction to Programming articles.