Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Variables, Constants and Assignment

Most programs work on the principle of inputprocessoutput. The input may be


data that is supplied in the program code (for example the word hello in the sayhello
program was data supplied in the program code). The problem with data provided
using this method is that each time the program runs it will make use of the same data.
There are two other methods of inputting data that mean that each time a program is
run it can make use of different data (thus providing different outputs each time it is
run):

 Get data from a file


 Get the user to enter data while the program is running.

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:

Integer Used to store whole numbers (including negative numbers).


Real Used to store numbers, including numbers with a decimal point and
fractional part.
Char Used to store a single character.
String Used to store a sequence of characters. A string can be thought of as an array of
characters – you will learn about arrays later.
Boolean Only two values can be stored in a Boolean variable – true or false.

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:

Trinity Catholic High School 1 N. Barnes 2002


Variables, Constants and Assignment

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}

String and character values need to be enclosed in single speech marks.

When you are using numeric data (e.g. integers and reals) there are several arithmetic
operators that can be used:

+ Used to add two numeric values together


- Used to subtract one numeric value from another
* Used to multiply one numeric value from another
/ Used to divide one numeric value by another. The result of this operation will
always be a real value and so cannot be stored in an integer variable.
DIV Used to divide one numeric value by another. The result of this operation will
always be the integer part of the division e.g. 11 DIV 4 is 2 remainder 3, so 2
would be the result of this operation.
MOD Used to divide one numeric value by another. The result of this operation will
always be the remainder part of the division e.g. 11 MOD 4 is 2 remainder 3,
so 3 would be the result of this operation.
Brackets can be used in expressions wherever needed, in the same way as they are
used generally in maths.

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 the program as percent.pas,


compile and run it. It looks like the
percentage displayed is not correct
but this is correct it is just not
displayed in a very nice way. The
result has been displayed in standard form (7.5 x 101) Change the line
writeln(‘Percentage mark is ‘,percentage);
to
writeln(‘Percentage mark is ‘,percentage:6:2);.

Trinity Catholic High School 2 N. Barnes 2002


Variables, Constants and Assignment

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.

Some key points about writing Pascal programs:

 A variable declaration section begins with the keyword var. It


contains a list of identifiers with a data type for each.
 Integer and real are two of the possible data types.
 To multiply two numbers in Pascal an asterisk (*) is used.
 To divide one number by another / is used.
 Extensive comments make a program easier to read. Any complicated
sections of code put a comment next to explaining what is going on.
 Put a short comment next to each variable in the variable declaration
section to explain how it will be used.
 At the start of your program you can place a comment saying who
wrote the program, when it was written and what the program does.
You might put the location where the file is saved in a comment.
 To assign a value to a variable ‘:=’ is used.

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.

Trinity Catholic High School 3 N. Barnes 2002

You might also like