 HubPages»
 Technology»
 Computers & Software»
 Computer Science & Programming
Guide to Data Types in Pascal
Why Learn Pascal?
Pascal is considered an old, longdead computer language by a lot of people. However, it is still recommended and thought in a lot of schools and elementary courses. This is because it is a great language to learn how programming works and it gives you a solid education so that you can continue to learn other language later.
Learning Pascal will also help you master Delphi which is an objectoriented language in which you can create your first desktop applications, games and useful tools and software. This article isn't a guide in which you will learn how to program, but will give you some basics and will teach you a lot about the many different data types in Pascal.
Integers
In mathematics we often use Z to categorize that a number belongs to the predefined type we know as Integers. In this set we have numbers ranging from [∞...4, 3, 2, 1, 0, 1, 2, 3....+∞]. As you can see an integer is either a negative or positive whole number or zero.
Numbers such as 5.55, 3.3333... or the square root of 2 are not integers.
Integers in Pascal
Type
 Range
 Size in Bytes


Byte
 0 .. 255
 1

Short Integer (Shortint)
 128 .. 127
 1

Small Integer (Smallint)
 32768 .. 32767
 2

Integer
 Either smallint or longint
 2 or 4

Long Integer (Longint)
 2147483648 .. 2147483647
 4

Int64
 9223372036854775808 .. 9223372036854775807
 8

Using Integers  an example
project FirstProgram; var a,i,sum: Integer; //declaring three integer variables begin sum:=0; //starting value of 'sum' readln(a); //user inputs the value for the variable a for i:= 1 to a do //a for loop from 1 to a begin sum:=sum+i; //calculates the sum of all numbers from 1 to a end; writeln(sum); //result output end.
Real Numbers / Floats
We saw that Integers were whole numbers, but what happens when we want to have a numbers such as 1.25 or even 3.333333...? Storing these numbers into a computer's memory used to be quite tricky, especially if they were an infinite array of numbers. Thus, a new data type was created  floating numbers > float or as they are called in Pascal > Real. This data type is used to display rational numbers that will probably have a decimal points and/or an exponent. As with the Integer data type, there are several Real types in Pascal with a different range and different memory requirements.
Real Data Type
Type
 Range
 Size in Bytes


Real
 Platform dependent
 4 or 8

Single
 1.5E45 .. 3.4E38
 4

Double
 5.0E324 .. 1.7E308
 8

Using Real  an example
program SphereVolume; var v,r:real; //volume and radius as real data types begin read(r); //input radius v:=(r*r*r*Pi*4)/3; //calculate volume; // Pi is a constant with a value of 3.14 writeln(v); //output end.
Characters and Strings
Characters and string come in many different data types and they can be very complex. There also many operation and functions you can do with them which opens up a lot of possibilities for a creative programmer. If you starting out, it is important to note that these data types represent ASCII characters. This means that if you had, let us say, a variable S that was string that had the value of '24' you could not use this variable for mathematical operation as this neither an integer not a real data type.
Chars and Strings in Pascal
Type
 Range


Char
 A single ASCII char

String
 255 ASCII chars

Ansistring
 No limit

Shortstring
 0 .. 255 (max) ASCII chars

Using chars and strings in Pascal  an example
program Changingastring; var C:Char; // a single character S:String; //our string n:integer; begin readln(C); readln(n); readln(S); S[n]:=C; // writeln(S); end.
Explanation of the above code
Strings work as a sort of array of characters and we can modify each element individually if we so desire. For example:
Our String S is 'ABCDEF' and has 6 characters in it. If we told our program:
writeln(S[3]);
This would output the letter "C". In the code above we input a character, a number and a String. Then we replace the a character in the string that has the position n with the character C.
Example
Input: +, 4, ABCDEF
Output: ABC+EF
Boolean Data Types
I saved what I find the easiest of the elementary data types for last and this is Boolean. This data type requires only one byte and can either be True or False. Bellow is a code that will, hopefully, explain this in more detail.
Using Boolean in Pascal  an example
program Trueorfalse; var check:Boolean; //our Boolean variable n,m,sum:integer; begin sum:=0; check:=False; //preset values Readln(n); Readln(m); If (n > 0) and (m >0) then check := True; //first if checker sum:=n+m; if (sum > 20) or (n*m < sum) then check := False; //second if checker If check = True then writeln('Our boolean is true!') else writeln('Our boolean is false!'); //final output checker end.
Further reading notes
 Lazarus
Lazarus download  a free pascal, opensource project  Danic Filip  Programming Help
Comments
No comments yet.