You are on page 1of 8

Key words and terms

Term Explanation
Constant A data item with a fixed value. It cannot be changed when the
program is executed, e.g. const pi := 3.1416
Variable The identifier associated with a particular memory location used
to store data. By using a variable you can store, manipulate and
retrieve data without knowing what the data will be, e.g. var
x := integer
Integer Any whole number, negative or positive e.g. 1, 34, 234564, -14
(there is a maximum value for each computer)
Real Any number represented with a fractional part, e.g. 3.1416, -
123.34
Char Any single character represented by the codes from the
character set in use on the computer.
String Textual data in the form of a list of characters, for example
words and punctuation. String data is made up of character data
and will usually vary in length.
Arithmetic and Variables

AS Computing - MSH
Constants
 Constant declarations appear between PROGRAM and
BEGIN and are heralded by the reserved word CONST.
Constants can be useful if you want to use the same
number or string continually in the program.
PROGRAM n (output);

CONST

pi = 3.14159;

BEGIN

writeln(‘pi is equal to: ‘, pi);

END.
 Separate arguments with a comma
Constants
PROGRAM volume;
These programs produce the
BEGIN same output. But notice the
improvements;
writeln(‘The volume of the shape is ‘, 4* 3 * 3);
writeln(‘The area of each end is ‘, 4 * 3); Transparency – the clear
relevance of each constant,
and hence the expressions is
END.
clearer.
PROGRAM volume;
Modification – the second
program can be more easily
CONST modified to apply to a
different box, we merely have
length = 3; width = 3; depth = 4;
to change the constant values.
BEGIN

writeln(‘The volume of the shape is ‘, length * width * depth);


writeln(‘The area of each end is ‘, width * depth);

END.
Variables
 Constants are fixed, if you want to be able to change the
value during run time, you need to declare variables.
i1, i2, i3, … , in : t;

Where i1, I1, i2, i3, … , in are


identifiers
And t is a type
 Variable declarations appear between constant
declarations and BEGIN, heralded by the reserved word
var. PROGRAM n (output);

CONST
pi = 3.14159;
VAR
x, y, z : integer;
BEGIN
User input
 We can assign values to variables at run time using the
read() statement.
read( v1, v2, … , vn);

Where v1, v2, … , vn are variables


possibly of different types

 When the statement is encountered at run-time program


execution halts until data is supplied. The user must then
type one value for each variable in the list.
 Each variable in the list is entered and terminated with a
space or end of line in order to move to the next variable.
Example
PROGRAM age;

VAR
agenow : integer;
BEGIN
readln(agenow);
writeln(‘Age after next birthday will be ‘,agenow+1)
readln();
END.

 Any problems with this program?


 If your program wants you to type something – make sure
it asks for it!!
 This is especially important if you are required to enter
several values
Exercises
Use constants and variables to write these programs.
1. Charlie is looking forward to retirement at 65, but cannot work out when
this will be. Write a program that will tell Charlie when how many years
he has to wait if he enters his current age.
2. Write a program which will print the number of electricity units used if
the present and previous meter readings are supplied.
3. Tom, Dick and Harry want to know their average age.
4. Write a program to convert a distance in feet and inches into meters and
cms.
5. When Charlie retires, he will be entitled to an annual pension of one
fifteenth of his salary for each complete year’s service. Write a program
to calculate this given the salary and years of service.
6. Write a program to print a payslip for Charlie when he supplies his
hourly rate of pay, the number of standard hours he has worked and his
over-time hours (over-time rate is 1.5x standard)

You might also like