Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views4 pages

Introduction To Data Types

The document provides an introduction to data types in Pascal, explaining the importance of declaring data types to prevent errors and manage memory. It classifies data types into static, dynamic, simple, structured, ordinal, and non-ordinal types, with a focus on basic types like integer, boolean, real, character, and string. The document also includes examples of how to declare and use these data types in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Introduction To Data Types

The document provides an introduction to data types in Pascal, explaining the importance of declaring data types to prevent errors and manage memory. It classifies data types into static, dynamic, simple, structured, ordinal, and non-ordinal types, with a focus on basic types like integer, boolean, real, character, and string. The document also includes examples of how to declare and use these data types in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to data types

A very simple definition:


The type of a data is the set of values it can take during the program. If it is attempted to give a value outside the set,
it will produce an error.
The assignment of types to data has two main objectives:
On one hand, detecting errors in the operations
On the other hand, determine how to execute these operations.
DePascalse says that it is a strongly typed language. This means that all data must have a declared type.
explicitly, and also that there are certain restrictions ontheexpressionsregarding the types of data that are involved in them.
One advantage of strongly typed languages is that much less effort is spent on debugging (fixing) programs.
thanks to the large number of errors it detectsthecompiler.
Data types, like almost all objects in Pascal, can be declared. The declaration of types has already been mentioned in the topic.
corresponding tothestructure of a program.

Classifications in data types


In Pascal, there are a great variety and quantity of data types. But in this tutorial, only the basics will be covered so that you can start.
building your first programs.
There are many classifications for types of data, and depending on the source you look at, they will show you one or another. Next,
you have one of the possible classifications.
Dynamic
Static
o The string type
o Structured
o Simple
. Ordinals
. Non-ordinals

Static types

Almost all types of data are static, the exception is pointers and they will not be addressed due to their complexity.
That a data type is static means that the size it occupies in memory cannot vary during execution.
program. That is, oncedeclareda variable of a determined type is assigned a fixed piece of memory, and this piece
it cannot be increased or decreased.

Dynamic types

Within this category only the pointer type enters. This type allows you to have greater control over memory management in.
your programs. With them, you can manage the size of your variables at runtime, that is, when the program is running
running.
Pointers may be the most complex concept when learning a programming language, especially if it is the first one.
What you learn. Because of this, we will not address it. Furthermore, languages that are very trendy (for example, Java) do not allow for
programmer work with pointers.

Simple types

As their name indicates, they are the basic types in Pascal. They are the simplest and easiest to learn. For all this, they will be in
those of us who focus.
The most basic simple types are: whole, logical, characteryreal. And most programming languages support them, right?
as happens with structures that can vary from one language to another.

Structured types

While a variable of a simple type only references a single element, structured types refer to collections of elements.
The collections of elements that appear when talking about structured types are very varied: we have ordered collections that
they are represented by the array type, unordered collections by the set type, and even collections that contain others
types, are the so-called records.

Ordinal types

Among the simple types, the ordinals are the most abundant. A type is said to be ordinal because the set of values
that represents can be counted, that is, we can establish a one-to-one relationship between its elements and the set of the
natural numbers.
Among the simple ordinal types, the most important are:
The integer type(integer)
The logical type(boolean)
The character type(character)
Non-ordinal types

Simplifying, we could reduce the non-ordinal simple types to the typereal. This type helps us declare variables that can
take values within the set of real numbers.
Unlike ordinal types, non-ordinal ones cannot be counted. A one-to-one relationship cannot be established between them and
the natural numbers. In other words, for a set to be considered ordinal, it must be possible to calculate the position, the
the previous element and the next one of any element of the set. What is the successor of 5.12? It will be 5.13, or 5.120, or 5.121, ...
The basic types that interest us
After looking at one of the possible classifications for data types, let's move on to those that interest us: the simple types.
We will really see the most basic simple types, which are: integer, boolean, characteryreal. Additionally, we will also talk about a
a little about character strings, the so-calledstrings.
Note: below will only be commented on what each type is, its declaration will not be explained, you can see this if you go tothesection
corresponding.

The integer type


As you may have read, the integer data type is a simple type, and among these, it is ordinal.ldeclarean integer type variable,
you are creating a numeric variable that can take positive or negative values, and without a decimal part.
You can use this type of variables in assignments, comparisons, arithmetic expressions, etc. Some of the most important roles
common ones that develop are:
Control aloop
Use them as a counter, increasing their value when something happens.
Perform integer operations, that is, without a decimal part.
And many more...
Below you have an example in which two integer variables appear. As you can see, the example shows both.
ways to declare a variable of integer type:

type
integer
var
i : tCounter;
n : integer;

begin
n := 10; (* we assign a value to the maximum *)
i := 1; (* we assign a value to the counter *)

while (i <= n) do begin


writeln('The value of i is ',i);
i := i + 1
end
end.

The boolean type (logical)


The logical data type is the one that allows you to use variables that have only two possible values: true or false. Because of this, its
The utility is obvious, and it is none other than check variables. They help us maintain the state of an object through two
values
yes/no
true/false
works/does not work
on/off
etc.
To clarify, let's look at an example:

type
tLogico = boolean;
var
it rains: tLogical; (* if it rains or not *)
boolean; whether I find the umbrella or not
*)

begin
(* here the values of 'it rains' would be determined and
umbrella
if it rains and (not umbrella) then
I stay at home
else
I'm going to take a walk
end.

The real type


As you have seen, Pascal supports the integer number set. But it's not the only one, it also allows you to work with numbers.
belonging to the real set.
The type of real data corresponds to real numbers. This is an important type for calculations. For example in
the statistics, as they are characterized by primarily dealing with decimal values.
Note: Although you may be used to writing decimals with a comma, I warn you that in Pascal and in all programming languages
programming is written with a point. For example: 3.1416
Below you have an example in which the real type is used. In it, you can see the two ways to declare a real variable, and
also the use of a real constant. Just in case you are curious, the result of running the compiled program is:
The area for a radius of 3.14 is 63.6174

const
pi = 3.1416;
type
tArea = real;
var
A : tArea; (* area *)
R : real; (* radio *)

begin
R := 4.50;
A := pi * R * R; (We calculate the area)

write('The area for a radius of ', R:4:2,' is ', A:8:4)


end.

The types charystring (character and string)


With the character type, you can have objects that represent a letter, a number, etc. That is to say, you can use variables or constants that
they represent an alphanumeric value. But be careful, each variable can only store a single character.
However, with strings you can contain more than one character in a single variable. For example, you can
store your name in a variable.
Let's see how both types are used in the following example

type
tName = string[10]; (* can store 10 characters
*)
var
name tName; (* variable to store the
name *)
char; (** character to contain the
NIF letter *

begin
name Me
L

My name is ,name, and my letter is


,letter_NIF
end.

You might also like