Variables, Constants and Assignment
Variables, Constants and Assignment
Any data got from a file or from the user needs to be store in a variable. A variable is
an area of memory, that has been given an identifier (name/label), which can be used
to store one piece of data.
Before a variable can be used it has to be declared. This is done in Pascal using the
var keyword. Each variable is given a name and you also need to state what type of
data will be stored in the variable. Here is an example of declaring a variable:
Var total: integer; {this declares a variable, called total, that is used to store an integer
(whole number) value}
There are several basic data types that can be used in Pascal:
Exercise
Write down the statements that would be used to declare the following variables:
a) an integer called count;
b) a string called name;
c) three real variable called average, deviation and variance.
After declaring a variable you are able to use it in your program. To put data into a
variable you use an assignment statement. The examples below put the value 10 into
the count variable and Gertrude into the name variable:
count:=10;
name:=’Gertrude’;
:= is the assignment operator and it gives the variable on its left-hand side the value of
the expression on its right-hand side. The next examples are also valid assignment
statements:
count:=total; {count becomes equal to the value stored in the variable total (only likely to
be a valid assignment statement if count and total are of the same data type.}
count:=count+1; {count becomes equal to the value one larger than its current value}
When you are using numeric data (e.g. integers and reals) there are several arithmetic
operators that can be used:
The next program you are going to write will calculate the percentage mark for an
exam. Select new from the file menu. Type in the following program:
program percent;
{Written by put your name here on put today’s date here
This program calculates the percentage mark scored in an exam.}
var
mark :integer; {the mark scored in the exam}
max_mark :integer; {the maximum possible mark that could be
scored in the exam}
percentage: real; {used to store percentage mark scored in the exam}
begin
mark:=45;
max_mark:=60;
percentage:=mark/max_mark*100;
writeln(‘Percentage mark is ‘,percentage);
readln;
end.
Save, recompile and run. This time the percentage is displayed in an easier to read
way. Notice that in a writeln statement, as well as displaying text, you can also
display the contents of a variable.
In percentage:6:2 the 1st number states the minimum number of spaces to display the
result in and the 2nd number states the number of decimal places to be used.
Exercise 2
Change the maximum mark that can be scored in the exam to 62. Recompile and
run.
If you want to have a value that you do want to stay the same throughout the program
you can use a constant instead of a variable to store the value. Constants have also
got to be declared before use at the beginning of the program, e.g.
const pi=3.14;
The advantage of using a constant instead of a variable is that the value cannot be
accidentally changed when the program is running and also the program will run
fractionally faster as all references to the constant are replaced by the actual value
when the program is compiled – this cannot be done with variables as their value can
change as a program runs. The advantage of using a constant in your programming
code instead of the actual value is that if the program needs to be updated so that the
constant has a different value (perhaps a more accurate version on pi is needed) then
you only have to change the value once (in the constant declaration). If you had used
the actual value in your program code then you would need to update each instance of
the actual value in your program code. Another advantage is that it is easier for
humans to understand what a program is doing if statements like:
area = pi * radius * radius
are used rather than:
area = 3.14 * radius * radius
VAT is often stored as a constant.