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 details.

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.

eg.
var

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 lesson.

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


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