What is a variable

What is a variable?

    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.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

Type

    Along with the name we have to choose the type. It can not be changed after that. If we want to assign a value from another type, first we have to convert that value, if possible.

Name

    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).
 - The name cannot start with a number.
 - It cannot be a keyword(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.
 - price
 - studentNames
 - 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.

Size

    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 ;)

Value

    The one, that we assigned. We can not use the value before we assign it.

Address

    It is automatically given by the computer. Usually it is not visible to us. We will talk about addresses when we get to pointers.

Creating/defining

    It is easy – just write the type, followed by its name. From there on you can work with it.

int iNumber; - 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.

Assigning a value

    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.

Variables example - calculating triangle area.

Homework

  1.  Answer the questions:
    - What is a variable?
    - Can we change its type?
    - What characters are allowed in its name?
  2. Create an algorithm which allows the user to input his name and age. Remember the values in variables and output them back to the user.
  3. Create a plan for a program that calculates the perimeter of a rectangle. The formula is P = (sideA + sideB) / 2
  4. Write a flow chart that calculates the area and perimeter for a circle. The user inputs a diameter (d). Some clues:

    radius r = d/2; Pi = 22/7; Perimeter P = 2 * Pi * r; Area = Pi * r * r;

    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.

Previous: Data type

Next: Math operators 

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› Variables

Tutorial Contents:

1)Learn Computer Programming
2)Software Development Process
3)Flow Chart
4)Flow Chart Symbols
5)Data Type
6)What is a variable
7)Math Operators
8)Logical Operators
9)Loops

10)Nested Loops
11)Arrays
12)Multidimensional arrays
13)Programming Questions

Did this help? Support me with your vote ;-)

Did this help?