What is a Variable?
Computer programs manipulate (or process) data.
A variable is used to store a piece of data for processing.
It is called variable because you can change the value stored.
More precisely, a variable is a named storage location, that stores a value of a
particular data type.
In other words, a variable has a "name", a "type" and stores a "value" of that type.
Name
Type
Size
Value
A variable has a "name" (or identifier), e.g., radius, area, sum. The name is
needed to uniquely identify and reference a variable, so as to assign a value to
the variable (e.g., radius=1.2), and retrieve the value stored
(e.g., area = radius*radius*PI).
A variable has a "type". Examples of type are:
o int: for integers (whole numbers) such as 123 and -456;
o double: for floating-point or real numbers, such as 3.1416, -
55.66, 7.8e9, 1.2e3, -4.5e-6 having a decimal point and fractional part, in
fixed or scientific notations.
A variable can store a "value" of the declared type. It is important to take note
that a variable is associated with a type, and can only store value of that
particular type. For example, a int variable can store an integer value such
as 123, but NOT real number such as 12.34, nor texts such as "Hello". The
concept of type was introduced into the early programming languages to
simplify interpretation of data.
The above diagram illustrates 2 types of variables: int and double. An int variable
stores an integer (whole number). A double variable stores a real number.
To use a variable, you need to first declare its name and type, in one of the
following syntaxes:
// Syntax: Declare a variable of a type
var-type var-name;
// Example:
int sum;
double radius;
// Syntax: Declare multiple variables of the same type
var-type var-name-1, var-name-2,...;
// Example:
int sum, difference, product, quotient;
double area, circumference;
// Syntax: Declare a variable of a type, and assign an initial value
var-type var-name = initial-value;
// Example:
int sum = 0;
double pi = 3.14159265;
// Syntax: Declare multiple variables of the same type with initial values
var-type var-name-1 = initial-value-1, var-name-2 = initial-value-2,... ;
// Example:
int firstNumber = 1, secondNumber = 2;
Take note that:
Each declaration statement is terminated with a semicolon (;).
In multiple-variable declaration, the names are separated by commas ( ,).
The symbol =, known as the assignment operator, can be used to assign an initial
value (of the declared type) to the variable.
Once a variable is declared, you can assign and re-assign a value to a variable, via
the assignment operator "=". For example,
int number; // Declare a variable named "number" of the type "int"
(integer)
number = 99; // Assign an integer value of 99 to the variable
"number"
number = 88; // Re-assign a value of 88 to "number"
number = number + 1; // Evaluate "number + 1", and assign the result back to
"number"
int sum = 0; // Declare an int variable named sum and assign an
initial value of 0
sum = sum + number; // Evaluate "sum + number", and assign the result back
to "sum", i.e. add number into sum
int num1 = 5, num2 = 6; // Declare and initialize two int variables in one
statement, separated by a comma
double radius = 1.5; // Declare a variable name radius, and initialize to
1.5
int number; // ERROR: A variable named "number" has already been
declared
sum = 55.66; // WARNING: The variable "sum" is an int. It shall not
be assigned a floating-point number
sum = "Hello"; // ERROR: The variable "sum" is an int. It cannot be
assigned a text string
Take note that:
Each variable can only be declared once.
You can declare a variable anywhere inside the program, as long as it is declared
before it is being used.
Once the type of a variable is declared, it can only store a value belonging to this
particular type. For example, an int variable can hold only integer such as 123, and
NOT floating-point number such as -2.17 or text string such as "Hello".
The type of a variable cannot be changed inside the program.
x=x+1? x+y=1?
Assignment (=) in programming is different from equality in Mathematics. e.g., "x=x+1"
is invalid in Mathematics. However, in programming, it means compute the value
of x plus 1, and assign the result back to variable x.
"x+y=1" is valid in Mathematics, but is invalid in programming. In programming, the
RHS of "=" has to be evaluated to a value; while the LHS shall be a variable. That is,
evaluate the RHS first, then assign to LHS.
Some languages uses := as the assignment operator to avoid confusion with equality.