Sei sulla pagina 1di 13

Basic Variables in Pascal

A variable is an expression which represents a value. A variable is named such because it can
have any value - its value is variable.

There are many different types of variables. For the moment we will say that a variable can store
a word or a number.

eg. Number is a variable which stores a number.

Number is assigned a value of 23456.
Therefore later in the program saying:
5 * Number + 6 will be the same as saying:
5 * 23456 + 6, since Number is equal to 23456.

In Pascal, assigning variables is done by:

Number := 23456;
This will make the variable, 'Number', equal to 23456.

There are several different types of number variables:

*Integer -Any Integer (Whole number) from -2^15 to 2^15 - 1

*LongInt -Any Integer (Whole number) from -2^31 to 2^31 - 1
*Real -Any Real number (Number which can have decimal points)
from 2.9 * 10^-39 to 1.7 * 10^38
(It is accurate to 11 significant figures)

Plus there are a few others, (which we won't mention yet). It isn't necessary to know all the

All you really need to know is that Integer variables store Integers. LongInt variable can hold
much larger Integers, and Real variables can hold any number.

There are two main types of variable which hold letters:

*Char -Any single character

*String -A word or phrase up to a set length

A string is a series of characters. The default maximum length of a string in Pascal is 255
characters. However if you don't want your string to be that big you should set it smaller. You do
this by putting the length of the string in square brackets after the variable.

Pascal also has variable of type 'Boolean'. These can have the values of either true or false.
In Pascal, variables are declared at the beginning of the program or procedure. They must be
declared after the keyword 'var'. Variables are declared in two parts. First is then name of the
variable - how it will be referred to in your program. This is followed by a colon. Then is the
type of variable, eg Integer, Byte. This is followed by a semicolon.


myInt : Integer;
aRealNumber : Real;
thisIsAString : String[30];
booleanVariable : Boolean;

This will create the folling variables:

myInt - This will be an Integer.
aRealNumber - This is a Real number.
thisIsAString - This is a sequence of letters with a maximum length of 30 characters.
booleanVariable - This contains a true or false value.

It is interesting to note that 'string' is a keyword. This is because it is different to other variable
types, as it contains a sequence or 'Array' of characters. We will study Arrays in depth in a latter

Make sure you understand all of the above before proceeding further.

Previous Lesson(Program Structure) Main Menu Next Lesson(Basic I/O)

Pascal can do many mathematical operations. They are all relatively simple and easy to
The first thing to remember is that pascal uses := not = to assign a value to a variable.
e.g. int := 3;
Basic mathematical operators
Addition ...................... x := y + z;
Subtraction ................... x := y - z;
Multiplication ................ x := y * z;
Division ...................... x := y / z;
Integer division .............. x := y div z;
Modulo arithmetic ............. x := y mod z;

Integer Division: One integer is divided by another and the integer part of the result is returned.

Modulo Arithmetic (Remainder Arithmetic): x := y mod z;

The above finds the remainder of y/z and puts it into x.

These mathematical operations are pretty self explanatory.

Mathematical functions


SQR(Real Variable)

SQR returns the square of the real variable that is passed to it, pretty simple really.

x := SQR(y);
This finds the square of y and puts the result in x.



SQRT(Real Variable)
SQRT returns the square root of the real variable that is passed to it, pretty simple really.

x := SQRT(y);
This finds the square root of y and puts the result in x.



SIN(Real variable)
SIN returns the sin of the number that is passed to it. Unfortunately this is in radians(stupid
2*pi radians is equal to 360 degrees, so to convert from degrees to radians it is degrees/180 * pi,
and from radians to degrees it is radians/pi * 180. It is a bit of a hassle but nevermind.

x := SIN(y);
This finds the sin of y(radians) and puts the value in x.



COS(Real variable)

COS returns the cos of the number that is passed to it. This is also in radians. If you want to
know how to convert
radians into degrees and vice-versa then read the explanation of SIN.

x := COS(y);
This finds the cos of y(radians) and puts the value in x.



ARCTAN(Real variable)

ARCTAN returns the inverse tanget of the number that is passed to it.
It returns the angle in radians (gasp).

x := ARCTAN(y);
This finds the inverse tangent, in radians, of y and puts the value in x.


To find tanget just divide sin(Y) by cos(Y).

e.g x := sin(y)/cos(y); finds the tangent of y and puts it in x (remember radians).

To find INVERSE SIN or INVERSE COS do the following...

INVERSE SIN = ARCTAN(y/sqrt(1-sqr(y)))
INVERSE COS = ARCTAN(sqrt(1-sqr(x))/x)

So x := arctan(y/sqrt(1-sqr(y))); finds the inverse sin of y and puts it in x.

So x := arctan(sqrt(1-sqr(x))/x); finds the inverse cos of y and puts it in x.

Previous Lesson(Basic I/O) Main menu Next Lesson(Procedures)

Procedures and Functions

Sometimes when you are programming you might need to use the same piece of code over and
over again. It would make your program messy if you did this, so code that you want to use
multiple times is put in a procedure or a function.

The difference between a function and a procedure is that a function returns a value whereas a
procedure does not. So if your program has multiple Yes/No questions then you might want to
make a function which returns Yes or No to any question.

Procedures and Functions both take parameters. These are values that the procedure is passed
when it is called. An example of this would be...

... drawBob (x,y); ...

This would call the procedure drawBob. It passes it the values x and y which the procedure will
use as Bob's coordinates. The drawBob procedure would look something like this...

procedure drawBob (x,y : integer); begin <Code here> end.

Somewhere in the code it would use x and y. Notice that x and y are declared like variables
except for the fact that they are in the procedure's header. If you wanted different types of
parameters, eg. integers and strings, then you must separate them by semi-colons like this...

procedure doSomething (x,y : integer; name : string);

<variables used in the procedure>
<Code here>
There might be a situation where you want the procedure to modify the values passed to it.
Normally if you did this in the procedures it modifies the parameters but it does not modify the
variables that the parameter's values were given from. Anyway if you want to do this you need to
put var in front of the parameters. So if you wanted to do something which changed x and y you
would make a procedure like so...

procedure modifyXY (var x,y:integer);

<Modification of x and y>

So by now you should know how to use a procedure. Next we will talk about Functions.

A function is like a procedure except it returns a value. You could also think of a function as a
dynamic variable, ie it gives itself a value when you have a reference to it. An example function
is shown below which returns the day name from a number between one and seven. The numbers
represent the days in the week of course.

function dayName (dayNumber : integer): String;


case dayNumber of
1 : dayName := 'Monday';
2 : dayName := 'Tuesday';
3 : dayName := 'Wednesday';
4 : dayName := 'Thursday';
5 : dayName := 'Friday';
6 : dayName := 'Saturday';
7 : dayName := 'Sunday';

Notice dayName assigns itself a value. The type of value that is to be assigned to it is declared in
the Function header after the parameters by going, ': <variable type>' which is in this case,

You should know enough about procedures and functions now so this is where this lesson ends.

Previous lesson (Basic Maths) Main Page Next lesson (Loops)

Okay, this lesson you are going to learn about loops. There are two main types of loop in Turbo
Pascal. While loops and For loops.

While loops take the form :

while <condition> do
statement block
These are useful for things like waiting until the user presses a key or waiting until a number
entered by the user is valid.

For instance you might say :

valueIn : String; {This will be the value the user enters}
I, code : Integer;
valueIn := 'abc'; {Fill it with a non integer value}
code := 1;
while code <> 0 do {While code is zero do the following}
begin writeln('Enter a number:');
val(valueIn, I, code);
{Move the integer value of valueIn to I or if it isn't an}
{integer then code will be something other than 0. NB: code is}
{the error code. I can only hold integers so there is an error}
{raised when the string can't be converted to an integer}

In this loop as long as condition is true (in this case code <> 0) it will execute the code between
the begin and the corresponding end. You can miss out the begin and end but then you will only
be able to have one line of code, or even no lines of code at all if you want.

eg: while not keypressed do;

In the above example as long as there is not a key pressed the instructions in the loop are
executed. Since there are no instructions in the loop this stops the program until the user presses
a key.

The 'For' Loop

This loop takes the form:

for variable := value1 [to|downto] value2 do
statement block
For loops are more useful for iterating through an array. For instance you might want to add 1 to
each integer in an array...
i : Integer; {This will be the counter for the loop}
myArray : array[1..30] of Integer; {See arrays for this}
for i := 1 to 30 do
myArray[i] := myArray[i] + 1;
This does goes through each item in the array 'MyArray' and adds one to the current value in it.


The repeat loop is very similar to the while loop except the condition is at the end, and if you
want to execute multiple lines, then you don't need to put begin .. end keywords around the
multiple lines. For instance, this loop using while...
i := 1;
while i <= 6 do
i := i + 1;

Would be like this with a repeat loop...

i := 1;
i := i + 1;
until i > 6;

Notice how the condition has changed slightly. For the while loop, it would do the loop while the
condition is true, whereas the repeat loop does it until the condition is true. (Or while the
condition is false).
Previous Lesson(Procedures/Functions) Main Menu Next Lesson(Flow Control)

Flow Control
Flow control is basically changing what your program does depending on the circumstances. In
pascal there is the if statement which is used like so:

if <condition> then
statement block
else if <condition> then
statement block

The condition takes the form:

[boolean expression (AND|OR) [NOT] boolean expression ...]

and this can go on for a long time. For instance you could have a condition:

if (i = 1) and (j <= 3) or not (k <> 0) then

statement block

Note : (not k <> 0) is the same as (k = 0).

Now here is an example program:

program security;

uses crt;
input : String;
writeln('Enter the password');
if (input = 'Pascal') then
writeln('Pascal is easy!')
{Note no semicolon for one line}
else if (input = 'Basic') then
writeln('Basic is not');
writeln('very hard!');
writeln('Wrong password!');

This first prompts the user to enter a password and then reads that into intput. First it checks to
see if input is 'Pascal'. Note that this is case sensitive. If it is 'Pascal' then it writes 'Pascal is
easy!' and goes to the end of the if statement (which happens to be the end of the program)
otherwise it checks the next condition. Is it 'Basic'? if it is then write 'Basic is not very hard!'
(over two lines).If it is not then try the next condition. The next condition is ELSE. The code in
the ELSE part of the if statement is executed if none of the other conditions in the if statement
are met.
Another flow control command, which is actually considered bad practice to use, but is quite
useful in some situations is goto. To use this declare a label in the label section and use it in your
code. Then just say : goto label;

label label1;

{Note the colon!}
goto label1;
{Note no colon}

You might have noticed that sometimes the if statement may get a little cumbersome to use. ie:
if i = 0 then
else if i = 1 then
else if i = 2 then
And so on. This is really cumbersome and annoying (you have to type the same thing over and
over). So what you want to use is the case statement. The above example would be done like so:
case i of
Very handy. If you want to have multiple lines of code for one of the options, then you must put
the multiple lines between begin and end. If you try to use the case statement with a String type
(or any type that isn't a char or integer) then Pascal will give you an error. You can only use
ordinal types with the case statment.
Another useful set of commands are the 'EXIT' commands. These are:

This does the simple task of ending your program.

This command exits from the current procedure/function.

This command exits from the current loop.

Previous Lesson (Loops) Main Menu Next Lesson (Arrays)

Arrays are an important part of Pascal. You can think of them like a catalogue of information.
Arrays are declared under the type section. They are a collection of a number of variables,
arranged in the form of a table.


integerArray = array[1..30] of integer;

integerTable = array[1..25,1..25] of integer;
stringArray = array[1..100] of string[15];

This is an example of declaring an array in the type block. The array called 'integerArray' is like
a list of integers, if seen on paper it might look like this...


The array called integerTable can be represented by a table. It is a two dimensional array. You
can have three,four or even five dimensions in an array if you want.

Accessing Arrays
To access an entry of an array during the program you must go...arrayname[entrynumber]
or to access a multi-dimensional array you go...arrayname[x,y,z] (3d array)


Program classTest;
{An example program demostration arrays}
{Takes scores from a class test and tells people if they passed or not}

uses Crt;


testScores = array[1..10] of integer;


i : integer;
marks : testScores;
passOrFail : string[6];


for i := 1 to 10 do
begin write ('Please enter test score number ',i,': ');
for i := 1 to 10 do
if (marks[i] < 50) then
passOrFail := 'Failed';
passOrFail := 'Passed';
writeln ('Student no.',i,' ',passOrFail);

while not keypressed do;


Arrays can also be declared in the variables section without making a type. However it is better
to use a type if you plan to use the same type
of array in multiple places. So this code in the
last program...


testScores = array[1..10] of

marks : testScores;

Can also be written like this:


marks : array[1..10] of integer;

This would do exactly the same thing, but does not declare a type of 'testScores'.

Previous Lesson(flow control) Main Menu Next Lesson(records)