It’s fun to call them primitives because it makes you feel smart but what we are talking about are the most basic things in a program.
Values
The most basic thing in a program is a value. It is what it is. It can be a number, 5 if my favourite, a letter like K or k or perhaps a decimal like 0.7. Or maybe a boolean value like 1 or 0. It is only one thing. It is a single answer to a question. It is not a word, those a strings, which is a list of values.
Variables
Variables are placeholders for values and they are borrowed from maths, algebra to be exact. So ‘x’ in a maths formula is a variable. It is a label for a value that we don’t necessarily know at the time. Think of it as an envelope with a name written on it.
Types
Variables and values can be of different types. Here are some normal types:
- Integer: a whole, positive number, like 5. It could also be a negative number, depending on your language.
- Floating point, or Decimal: like 18.2.
- Character: a letter, like K or k.
- String: a collection of characters, in order, like I like Ike. They are usually written in quotes, to make it clear that you are typing a string, not a command. So, “I like Ike”.
- Boolean: Yes/No, True/False, 1/0. There can only ever be two values and these are used for making decisions.
Programming languages can be divided into those that are strongly-typed or loosely-typed. In a strongly-typed language you have to tell the computer what type each value or variable is in advance, or you get an error. In a loosely-typed language you can just write stuff and the computer will usually guess the type correctly.
Operators
Now we have a value, a variable to store it in and we know what kind of value it is. To do anything with it, we use an operator. Operators are the most basic commands and they do basic things. At their most basic, we have arithmetic. These are the same in almost all languages:
- + is plus (or sum)
- – is minus (or difference)
- * is multiplication
- / is division
- % is modulus, but don’t worry about that for now.
Each operator appears between two values or variables, like x + 1, usually integers or decimals, and they give the arithmetic result, another integer or decimal. Then we have comparitors, which compare values.
- = checks if two values/variables are equal .
- == checks if two values/variables are the same, not the same thing as equal, but you don’t have to worry about that at the moment. I’m just putting it in to keep you alert.
- > Greater than.
- < Less than .
- <= Equal to or greater than.
- >= Equal to or less than. These all work great for integers or decimals, but get odd results for characters or strings.
All these comparitors need two values or variables to check. Like is x = 5. The result isn’t a number, it’s a boolen, which we usually think of as true or false.
So now we can do maths and compare things, there’s one other kind of operator, string operators:
- = checks if two strings are the same. This can be really tricky. Is the integer 5 the same as the 5 in “I have 5 donkeys.”? Some languages say yes, some say no because the values are different types. This is a really, really common difficulty in programming and is why we have strongly-typed languages in the first place.
- + or & is used to add strings together, or we say concatinate two strings. What it does is glue the second string to the end of the first string.
Did I say one other kind of operator? There’s one more and it’s a big one. Assignment.
To assign a value to a variable we normally use an equals sign. In the old days they would use the command Let, which is borrowed from maths:
Let x =5
This tells the computer that you are declaring a new variable, x, and assigning it the value 5. If you want to change the value later you write:
x = 9
These days most languages will let you just write this at the start and it will automatically create the variable x when you first assign a variable to it.
So now we can do things with maths and with words (strings). If you add If statements you have enough to write any program. The trouble is that your program would be really long and repetitive. All languages have other commands to make life easier and more rational.
