Each variable, the concept of a variable is explained before, has a
certain type, that is the type of the value which it is to store. There
are 4 standard types in Pascal, which are integer, real, char and boolean. |
The declaration of a variable is done in the variable part of the program.
The format to be used is:
var |
identifier1 : type; {like a:integer;} |
|
identifierN : type; |
begin {not in variable declaration part anymore} |
|
The format of a variable, or in general identifier, in Standard Pascal
is as follows:
What you should not use as an identifier is a reserved word. Reserved
words are words which have a special meaning in Pascal, such as "begin",
"end", "and", "or", "not" etc.
Remember, that Pascal is not case sensitive, that is Name and name are
recognized as the same identifier. Moreover, Pascals identifiers are limited
to 8 characters, that is you can write more than 8 characters as a literal
name, but Pascal will cut off the longer variables to 8 characters. For
example "firsteight" and "firsteig" are recognized as the same identifierd.
Positive examples for identifiers are: |
first, count, name, give, henry18, henry6s. |
Negative examples for identifiers are: |
18henry, first.name, first-name, *jklpp etc. |
|
|
|
|
|
The integer datatype is used to represent integer numbers, that is
numbers which do not have fractions (eg. 13, -5). It has a range between
-maxint and maxint-1, where maxint is 32768. If you assign a larger number
than maxint to a number, maxint is subtracted from the number as many times
as needed to fit this number into the range and you can't detect the error.
So beware of this. |
Operators: '+' '-' '*' 'div' 'mod'. |
The syntax is:
integer1:= integer2 + integer3; |
integer1:= integer2 - integer3; |
integer1:= integer2 * integer3; {'*' stands for multiplication, don't
forget the limits -maxint and maxint} |
integer1:= integer2 div integer3; {div takes the integer part of the
division and assigns it to integer1} |
integer1:= integer2 mod integer3; {this is the standard modula operator} |
|
|
|
|
The Real Datatype |
The real datatype is to represent real values, but not all real values.
It's upper limit is 1.7E-38, where E-38 means a multiplicant of 10 to the
power -38, eg. 15.0E-4 = 0.0015. |
It's operators: +', '-', '*', '/'. |
real1:= real2 + real3; |
real1:= real2 - real3; |
real1:= real2 * real3; {'*' stands for multiplication, don't forget
the limits} |
real1:= real2 / real3; {this is the standard division we use everytime
in life} |
|
|
|
|
|
|
The Char Datatype |
|
The char datatype is used to store any single character value, like
'c', '8'. It can hold any of the letters, characters, punctuation marks
or digit characters that appear on the keyboard. Note that the character
'8' and the integer 8 are different. With the integer 8 you can perform
arithmetic operations whereas with the character '8' you can not. The character,
if it is written in the program, is enclosed in single quotes, where a
single quote is a '. However, if the input is a character from the
terminal, keyboard, then it is just written without quotes. |
|
The Boolean Datatype |
|
A boolean datatype can only have to values, either true or false. It
is used in conditional statements, (covered later) like if..then, while
etc. The assignment can be as follows: b:=true; or b:=(8<9); which is
also true. You should note that you should not check for equality of real
values, since even if they are theoretically equal, there can be an error
of Epsilon, the smallest number possible in Pascal, and then your equality
is false. |
|
|
Examples: |
The first example is an illustrative example showing how the memory
cell content holding the variable changes during an assignment.
|
As you see, contrary to the thoughts of most new pascal students, the
content of B does not change when A changes. |
|
Operator Precedence: |
|