What is a variable in computer programming? This is a name that keeps a value. For example the name 'price' holds the value 15(price = 15). That value is stored in the memory of the computer and we can access it when we need it.
Now that we know what is a variable, let's see how we can give it a name.
The name is a sequence of characters. We use it to assign a new value to it or to access the current value.
There are strict rules when giving a name. They may vary for different languages, but we cover the most popular ones(C, C++, C#, Java, PHP and others).
- Allowed characters: a,b.. z; A,B.. Z; 0,1.. 9; _ (this an underscore, not a dash/minus).
- The name cannot start with a number.
- It cannot be a (or reserved word) for the current language.
- In most of the languages it is case sensitive. “NUM1” is different from “num1”. There are exceptions like Pascal – it is not case sensitive.
Good naming practices
- The name has to be descriptive.
- Not too short, not too long.
- If the name consists of several words, separate them with an underscore or start each word with an uppercase
- Name constants in all UPPERCASE, separate words with an underscore.
Here are examples of good names for variables:
- count – Counts something.
- customer_Name, sCustomerName, customer_name
- MAX_VALUE – a constant.
Examples of bad names:
- 1name : can’t start with a number.
- break : A reserved word. This depends on the language.
- sStudent-Names : The dash “-“ is not allowed character.
- n : Not descriptive.
- iNumberOfStudentsPassedTheMainTest : :-D . Tooo descriptive, too long.
It is the type of data saved in our variable.
Some languages allow the change of the data type of a variable. One example for such language is php.
Languages like C, C++ and Java are "type-safe". This means the once a variable is declared, its data type cannot change. If we want to assign a value from another type, first we have to convert that value, if possible.
What is a variable's size? If you remember from the last lesson, different data types take different amount of memory. The size of the variable depends on the type of data that it keeps and sometimes on the value. Always keep this to the bare minimum. Don’t waste memory, but don’t worry if you still don’t know what this means ;)
The one, that we assigned. We can not use the value before we assign it.
It is automatically given by the computer. Usually it is not visible to us. We will talk about addresses when we get to pointers.
It is easy – just write the type, followed by its name. From there on you can work with it.
Creating a variable of type "int" and name it "iNumber".
In this tutorial we aim to learn the concepts of computer programming. We don’t care much about syntax, right now. That is why we will not explicitly create variables, we accept that they are created the first time when we use them.
After you know what is a variable and create one, you need to assign it with a value. To do that, put the name on the left side of the assignment operator “=” and the value on the right side. The right side can be any valid constant, operation, statement or variable that returns a value.
This is how it works: The computer looks at the right and “sees” that it is an equation. It calculates that side and receives a result. Then it takes that result and assigns its value to the left side. If the computer can’t solve the right side for some reason, there will be an error and the program will stop.
- Using a constant. Assigning "count" to 5:
count = 5;
- By the value of another variable.
number1 = 5;
number2 = number1;
- Using an equation.
area = (sideA * heightA) / 2;
Let’s do several algorithms with the new knowledge. The “area” equation
above calculates the area of a triangle, by given side and its height.
Task: Create a flow chart, describing the algorithm to calculate the area of a triangle.
If you are stuck with some of the tasks, feel free to review the lesson again or also any of the previous lessons. Once you have created the flow charts for these three tasks, you are ready to continue with the next step of the computer programming tutorial.
2)Software Development Process
4)Flow Chart Symbols
6)What is a variable