Professional Documents
Culture Documents
L03 - Arithmetic
L03 - Arithmetic
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
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
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;
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);
VAR
agenow : integer;
BEGIN
readln(agenow);
writeln(‘Age after next birthday will be ‘,agenow+1)
readln();
END.