Data is general concept for everything with which it operates Calculating machine. Any data type defines the set of values ​​that a particular variable can take and the operations that can be applied to them. Each variable encountered in a program must be associated with one and only one type.

There are two types of simple types in Pascal: ordinal types and real types. An ordinal type is either defined by the programmer (enumeration type or range type) or is denoted by the name of one of three predefined ordinal types: Boolean, Integer, or Char. A real type is denoted by the prescriptive type name Real.

An enumerated type is characterized by the many different values ​​it contains, among which a linear order is defined. The values ​​themselves are designated in the definition of this type by names.

A range (limited) type is specified using minimum and maximum values ​​related to a previously described ordinal type. This is how a new ordinal type is generated.

Ordinal data types

An ordinal data type describes a finite and ordered set of values. These values ​​are mapped to the sequence of ordinal numbers 0,1,2,...; an exception is made only for ordinal integers, which map onto themselves. Each ordinal type has a minimum and maximum value. For all values ​​except the minimum there is a previous value, and for all values ​​except the maximum there is a subsequent value.

The prescribed functions succ, pred, ord accept arguments of any of the ordinal types:
succ(X) - gives the next ordinal value after X
pred(X) - gives the ordinal value preceding X
ord(X) - gives the ordinal number for X

For all ordinal types, there are relational operators = , = , and > , assuming both operands are of the same type.

Boolean type

A Boolean value is one of two truth values, denoted by the predefined names false and true .

There are the following logical operations that give boolean value when applying them to logical operands:
and - logical AND
or - logical OR
not - logical NOT

Also, any of the relational operations (= , = , > , in) produces a logical result.

Additionally, the boolean type is defined so that false

There are also predefined logical functions (i.e. functions that give a logical result):
odd(F) - true if the integer is F-odd and the result is false if F-even
eoln(F) - end-of-line check
eof(F) - end-of-file check

Integer type

The integer type includes a variety of integers.

When working with integer operands, the following arithmetic operations give integer values:
* - multiplication
div - integer part from division
mod - remainder of division
+ - addition
- - subtraction

Pascal also has a predefined constant called MaxInt, which contains the maximum value of an Integer and is equal to 32767

The whole result is also achieved by four important prescribed functions:
abs(I) - absolute value of the integer value I
sgr(I) - the integer value of I squared given that I trunc(R) - gives the integer part of the real number R
round(R) - returns a rounded integer. In this case: for R>0 means trunc(R+0.5), and for R

If I is an integer value, then:
succ(I) - gives the next integer value (I+1)
pred(I) - gives the previous integer value (I-1)

Character type (Char)

Char values ​​are the elements of a finite and ordered set of characters. Values ​​of this type are represented by a single character enclosed in single quotation marks (apostrophes). If the apostrophe itself is needed, it is written twice.
Examples: "*" "G" "3" """" "X"

    For the Char type, the following minimum assumptions apply:
  1. The decimal digits 0 through 9 are ordered according to their numeric values ​​and follow each other (for example, succ("5") = "6").
  2. There may be capital letters from "A" to "Z"; if so, then they are ordered alphabetically, but do not necessarily follow one another (e.g. "A"
  3. There may be lowercase letters "a" through "z"; if so, then they are ordered alphabetically, but do not necessarily follow one another (e.g. "a"

To map a given set of characters to serial numbers and back, there are two predefined functions:
ord(C) - gives the ordinal number of the character C in the mentioned ordered set of characters
chr(I) - gives the character with serial number I

For arguments of type Char, the predefined functions pred and succ can be defined as follows:
pred(C) = chr(ord(C)-I)
succ(C) = chr(ord(C)+I)

Comment. The preceding or following symbol depends on the specified set of symbols, so both of these relations are valid only if the preceding or following symbol exists.

Real type

Values ​​of real type are the elements of an implementation-defined subset real numbers.

All operations on real-type quantities are approximate, their accuracy is determined by the implementation (machine) you are dealing with. The real type is a simple type, it is not an ordinal type. Real values ​​do not have an ordinal number and for any of them there is no previous or next value.

If at least one of the operands is a real type (the other can be an integer), the following operations give a real result:
* - multiplication
/ - division (both operands can be integers, but the result is always real)
+ - addition
- - subtraction

There are prescribed functions that give a real result given a real argument:
abs(R) - absolute value of R
sqr(R) - R squared if the result is within the range of real numbers

And these prescribed functions give a real result with an integer or real argument:
sin(X) - gives the sine of X; X is expressed in radians
cos(X) - gives the cosine of X; X is expressed in radians
arctan(X) - gives the arctangent of X expressed in radians
ln(X) - gives the value of the natural (base e) logarithm for X, X>0
exp(X) - gives the value of the exponential function (i.e. to the power of X)
sqrt(X) - gives the value of the square root of X, X>=0

Warning. The functions pred, succ cannot be used for real arguments. You cannot use values ​​of a real type when indexing arrays, for control in a loop with a parameter, for determining the base type of sets, for indexing in a variant operator.

A data type defines a set of valid values ​​and a set of valid operations.

Simple types.

Simple types are divided into ORDINAL and REAL.

1. ORDERAL TYPES , in turn, there are:

a) whole

Pascal defines 5 integer types, which are defined depending on the sign and value that the variable will take.

Type name

Length (in bytes)

Range of values

32 768...+32 767

2 147 483 648...+2 147 483 647

b) logical

The name of this type is BOOLEAN. Boolean values ​​can be one of the Boolean constants: TRUE (true) or FALSE (false).

c) symbolic

The name of this type is CHAR - occupies 1 byte. The value of a character type is the set of all PC characters. Each character is assigned an integer in the range 0…255. This number serves as a code for the internal representation of the symbol.

2. REAL TYPES .

Unlike ordinal types, whose values ​​are always mapped to a series of integers and are therefore represented absolutely precisely in PC, the values ​​of real types define an arbitrary number only with some finite precision depending on the internal format of the real number.

Length of numeric data type, bytes

Numeric data type name

Number of significant digits of a numeric data type

Decimal order range of a numeric data type

2*1063 +1..+2*1063 -1

STRUCTURED TYPES

Structured data types define an ordered collection of scalar variables and are characterized by the type of their components.

Structured data types, unlike simple ones, define many complex values ​​with one common name. We can say that structural types determine a certain way of forming new types from existing ones.

There are several structuring methods. According to the method of organization and type of components in complex data types, the following varieties are distinguished: regular type (arrays); combined type (records); filetype(files); multiple type(s); string type(strings); in the Turbo Pascal language version 6.0 and older, an object type (objects) was introduced.

Unlike simple types data, data of a structured type is characterized by the multiplicity of elements forming this type, i.e. a variable or constant of a structured type always has multiple components. Each component, in turn, can belong to a structured type, i.e. nesting of types is possible.

1. Arrays

Arrays in Turbo Pascal are in many ways similar to similar data types in other programming languages. A distinctive feature of arrays is that all their components are data of the same type (possibly structured). These components can be easily organized and any one of them can be accessed simply by specifying a serial number.

The array description is specified as follows:

<имя типа>= array[<сп.инд.типов>] of<тип>

Here<имя типа>- correct identifier;

Array, of – reserved words(array, from);

<сп.инд.типов>- a list of one or more index types, separated by commas; square brackets framing the list are a syntax requirement;

<тип>- any type of Turbo Pascal.

Any ordinal types can be used as index types in Turbo Pascal, except LongInt and range types with the base type LongInt.

Nesting depth structured types in general, and therefore arrays, is arbitrary, therefore the number of elements in the list of type indexes (array size) is not limited, however, the total length of the internal representation of any array cannot be more than 65520 bytes.

2. Records

A record is a data structure consisting of a fixed number of components called record fields. Unlike an array, the components (fields) of a record can be various types. To make it possible to refer to one or another component of a record, the fields are named.

The structure of a post type declaration is:

< Nametype>=RECORD< joint venture. fields>END

Here<имя типа>- correct identifier;

RECORD, END – reserved words (record, end);

<сп.полей>- list of fields; is a sequence of sections of a record separated by a semicolon.

3. Sets

Sets are a set of objects of the same type that are logically connected to each other. The nature of the connections between objects is only implied by the programmer and is in no way controlled by Turbo Pascal. the number of elements included in a set can vary from 0 to 256 (a set that does not contain elements is called empty). It is the inconstancy of the number of its elements that sets differ from arrays and records.

Two sets are considered equivalent if and only if all their elements are the same, and the order of the elements of the set is indifferent. If all the elements of one set are also included in another, the first set is said to be included in the second.

The description of the set type is:

< Nametype>=SET OF< bases. type>

Here<имя типа>- correct identifier;

SET, OF – reserved words (set, from);

<баз.тип>- the base type of set elements, which can be any ordinal type except WORD, INTEGER and LONGINT.

To define a set, the so-called set constructor is used: a list of specifications of the elements of the set, separated by commas; the list is surrounded by square brackets. Element specifications can be constants or expressions of a base type, as well as a range type of the same base type.

4. Files

A file refers to either a named area external memory A PC or logical device is a potential source or receiver of information.

Any file has three characteristic features

    it has a name, which allows the program to work with several files simultaneously.

    it contains components of the same type. The component type can be any Turbo Pascal type, except files. In other words, you cannot create a “file of files.”

    length again created file is not specified in any way when it is announced and is limited only by the capacity of external memory devices.

File type or variable file type can be set in one of three ways:

< Name>= FILE OF< type>;

< Name>=TEXT;

<имя>= FILE;

Here<имя>- file type name (correct identifier);

FILE, OF – reserved words (file, from);

TEXT – name of the standard text file type;

<тип>- any type of Turbo Pascal, except files.

Depending on the method of declaration, three types of files can be distinguished:

· typed files (set by the FILE OF... clause);

· text files(defined by type TEXT);

· untyped files (defined by the FILE type).

About converting Pascal's numeric data types

In Pascal, implicit (automatic) conversions of numeric data types are almost impossible. An exception is made only for the integer type, which is allowed to be used in expressions of type real. For example, if the variables are declared like this:

Var X: integer; Y: real;

then the operator

will be syntactically correct, although there is an integer expression to the right of the assignment sign and a real variable to the left, the compiler will convert numeric data types automatically. The reverse conversion automatically from the real type to the integer type is impossible in Pascal. Let's remember how many bytes are allocated for variables of type integer and real: 2 bytes of memory are allocated for the integer data type integer, and 6 bytes for real. There are two built-in functions for converting real to integer: round(x) rounds a real x to the nearest integer, trunc(x) truncates a real by discarding the fractional part.

The lesson covers the main standard data types in Pascal, the concept of a variable and a constant; explains how to work with arithmetic operations

Pascal is a typed programming language. This means that the variables that store data are of a specific data type. Those. The program must directly indicate what data can be stored in a particular variable: text data, numeric data, if numeric, then integer or fractional, etc. This is necessary primarily so that the computer “knows” what operations can be performed with these variables and how to perform them correctly.

For example, the addition of text data, or as it is correctly called in programming - concatenation - is the usual merging of strings, while the addition of numeric data occurs bitwise, in addition, fractional and integer numbers are also added differently. The same applies to other operations.

Let's look at the most common ones in Pascal types data.

Integer data types in Pascal

Type Range Required memory (bytes)
byte 0..255 1
shortint -128..127 1
integer -32768.. 32767 2
word 0..65535 2
longint -2147483648..2147483647 4

You need to keep in mind that when writing programs in Pascal integer(translated from English as a whole) is the most frequently used, since the range of values ​​​​is most in demand. If a wider range is needed, use longint(long integer, translated from English as long integer). Type byte in Pascal it is used when there is no need to work with negative values, the same goes for type word(only the range of values ​​here is much larger).

Examples of how variables are described (declared) in Pascal:

program a1; var x,y:integer; (integer type) myname:string; (string type) begin x:=1; y:=x+16; myname:="Peter"; writeln("name: ",myname, ", age: ", y) end.

Result:
name: Peter, age: 17

Comments in Pascal

Notice how comments are used in Pascal. In the example comments, i.e. service text that is “not visible” to the compiler is enclosed in curly braces. Typically, comments are made by programmers to explain pieces of code.

Task 3. The population of Moscow is a = 9,000,000 inhabitants. The population of New Vasyuki is b=1000 inhabitants. Write a program that determines the difference in the number of inhabitants between two cities. Use Variables

Real data types in Pascal

Real numbers in Pascal and in programming in general are the name for fractional numbers.

Type Range Required memory (bytes)
real 2.9 * 10E-39 .. 1.7 * 10E38 6
single 1.5 * 10 E-45 .. 3.4 * 10E38 4
double 5 * 10E-324 .. 1.7 * 10E308 8
extended 1.9 * 10E-4951 .. 1.1 * 10E4932 10

The real type in Pascal is the most commonly used real type.

The above were presented simple data types in Pascal, which include:

  • Ordinal
  • Whole
  • brain teaser
  • Character
  • Listable
  • Interval
  • Real

To display the values ​​of variables of real type, formatted output is usually used:

  • the format uses either one number, indicating the number of positions allocated to this number in exponential form;
  • p:=1234.6789; Writeln(p:6:2); (1234.68)

    Along with simple types, the language also uses structured data types and pointers, which will be the subject of subsequent lessons on Pascal.

    Constants in Pascal

    Often in a program it is known in advance that a variable will take on a specific value and will not change it throughout the execution of the entire program. In this case, you must use a constant.

    The declaration of a constant in Pascal occurs before the declaration of variables (before the var service word) and looks like this:

    An example of a constant description in Pascal:

    1 2 3 4 5 6 const x= 17 ; var myname: string ; begin myname: = "Peter" ; writeln ("name: " , myname, ", age: " , x) end .

    const x=17; var myname:string; begin myname:="Peter"; writeln("name: ",myname, ", age: ", x) end.

    “Beautiful” output of integers and real numbers

    In order to ensure that after displaying the values ​​of variables there are indents, so that the values ​​do not “merge” with each other, it is customary to indicate through a colon how many characters need to be provided to display the value:


    Arithmetic operations in Pascal

    Order of operations

    1. evaluation of expressions in parentheses;
    2. multiplication, division, div, mod from left to right;
    3. addition and subtraction from left to right.

    Pascal Standard Arithmetic Procedures and Functions

    Here it is worthwhile to dwell in more detail on some arithmetic operations.

    • The inc operation in Pascal, pronounced increment, is a standard Pascal procedure that means increasing by one.
    • Example of inc operation:

      x:=1; inc(x); (Increases x by 1, i.e. x=2) writeln(x)

      More complex use of the inc procedure:
      Inc(x,n) where x is an ordinal type, n is an integer type; procedure inc increments x by n.

    • The Dec procedure in Pascal works similarly: Dec(x) - decreases x by 1 (decrement) or Dec(x,n) - decreases x by n.
    • The abs operator represents the modulus of a number. It works like this:
    • a: =- 9; b:=abs(a) ; (b=9)

      a:=-9; b:=abs(a); (b=9)

    • The div operator in Pascal is often used, since a number of tasks involve the operation of whole division.
    • The remainder of division or the mod operator in Pascal is also indispensable for solving a number of problems.
    • Noteworthy is Pascal's standard odd function, which determines whether an integer is odd. That is, it returns true for odd numbers, false for even numbers.
    • An example of using the odd function:

      var x:integer; begin x:=3; writeln(sqr(x)); (answer 9) end.

    • Operation of exponentiation in Pascal is missing as such. But in order to raise a number to a power, you can use the exp function.
    • The formula is: exp(ln(a)*n), where a is a number, n is a degree (a>0).

      However, in the compiler pascal abc Exponentiation is much simpler:

      var x:integer; begin x:=9; writeln(sqrt(x)); (answer 3) end.

    Task 4. The dimensions of a matchbox are known: height - 12.41 cm, width - 8 cm, thickness - 5 cm. Calculate the area of ​​the base of the box and its volume
    (S=width*thickness, V=area*height)

    Task 5. The zoo has three elephants and quite a few rabbits, with the number of rabbits changing frequently. An elephant is supposed to eat one hundred carrots a day, and a rabbit - two. Every morning, the zookeeper tells the computer the number of rabbits. The computer, in response to this, must tell the attendant the total number of carrots that need to be fed to the rabbits and elephants today.

    Task 6. It is known that x kg of sweets costs a rubles Determine how much it costs y kg of these sweets, and also how many kilograms of sweets can be bought at k rubles All values ​​are entered by the user.

    Typically, data when processed by computer is presented in the form of either individual quantities or in the form of their aggregates. One of the most important, and perhaps the most important, characteristic of a quantity is its type.

    What do Pascal data types define?

    Firstly, the possible values ​​of variables, functions, expressions, constants belonging to the original type; secondly, it determines the internal form of data representation in a computer; and thirdly, it defines the functions and operations performed on quantities that belong to one type or another.

    In the Pascal language, all variables that will later be used in the program must be declared in the description section, indicating their type. The constant mandatory type description leads to redundancy in the source code of programs, but at the same time this redundancy is an auxiliary tool in the process of program development - which is a necessary property of modern high-level languages.

    In Pascal, the following data types are distinguished:

    The table below shows the types of simple data types in Pascal and their characteristics:

    The following properties are characteristic of ordinal types included in the group of simple ones:

    1. An infinite set of values ​​of an ordinal type is a limited ordered set;
    2. The standard Ord function is applicable (returns the ordinal number certain value in a given type) to all possible ordinal types;
    3. To all kinds of ordinal types, when the need arises, you can apply standard features Pred and Succ, which return the previous and subsequent values, respectively;
    4. On occasion, you can apply the standard functions Low and High to any ordinal type, returning the smallest and largest values ​​of quantities, respectively of this type.

    Data type equivalence and compatibility:

    The concepts of type equivalence and compatibility are of no small importance in the Pascal language. Let us define these concepts. Two types TIP1 and TIP2 are said to be equivalent if one of the following conditions is met:

    1. TIP1 and TIP2 are the same type name;
    2. type TIP2 is described using type TIP1 using either an equality or a sequence of equalities:

    type TIP1 = Integer; TIP2 = TIP1; TIP3 = TIP2;

    Now let's introduce the concept of type compatibility. Types are said to be compatible if:

    1. these types are equivalent;
    2. they are either integer or real;
    3. one of the two types is interval, the other is its basic type;
    4. both types are interval with a common base;
    5. one of the two types is string, the other is character.

    Type compatibility restrictions can be circumvented using type casting:

    When a type cast is used on a variable reference, it is treated as an instance of the type, which is represented by a type identifier. The variable size (that is, the number of bytes the variable occupies) must be equal to the size of the type represented by the type identifier.

    In Pascal language variables are characterized by their type. A type is a property of a variable by which a variable can take on many values ​​allowed by that type and participate in many operations allowed on that type.

    A type defines the set of valid values ​​that a variable of a given type can accept. It also defines the set of valid operations on a variable of a given type and defines data representations in random access memory computer.

    For example:

    n:integer;

    Pascal is a static language, which means that the type of a variable is determined when it is declared and cannot be changed. The Pascal language has a developed system of types - all data must belong to a previously known data type (either a standard type created during the development of the language or a custom type defined by the programmer). The programmer can create his own types with an arbitrary complexity structure based on standard types, or already user-defined types. The number of created types is unlimited. Custom types in a program are declared in the TYPE section using the format:

    [name] = [type]

    The system of standard types has a branched, hierarchical structure.

    Primary in the hierarchy are simple types. Such types are present in most programming languages ​​and are called simple, but in Pascal they have a more complex structure.

    Structured types are built according to certain rules from simple types.

    Signposts are formed from simple types and are used in programs to set addresses.

    Procedural types are an innovation of the language Turbo Pascal, and they allow subroutines to be accessed as if they were variables.

    Objects are also an innovation, and they are intended to use the language as an object-oriented language.

    In the Pascal language, there are 5 types of integer types. Each of them characterizes the range of accepted values ​​and the space they occupy in memory.

    Using integers you should be guided by the nesting of types, i.e. types with a smaller range can be nested within types with a larger range. The Byte type can be nested within all types that occupy 2 and 4 bytes. At the same time, the Short Int type, which occupies 1 byte, cannot be nested in the Word type, since it does not have negative values.

    There are 5 real types:

    Integer types are represented absolutely precisely in a computer. Unlike integer types, the value of real types defines an arbitrary number only with some finite precision, depending on the format of the number. Real numbers are represented in a computer as either fixed or floating point.

    2358.8395

    0.23588395*10 4

    0.23588395*E 4

    The Comp type occupies a special position in Pascal; in fact, it is a large signed integer. This type is compatible with all real types and can be used for a large integer. When representing real numbers with floating point, the decimal point is always implied before the left or leading mantissa, but when operating on a number it is shifted to the left or right.

    Ordinal types

    Ordinal types combine several simple types. These include:

    • all integer types;
    • character type;
    • boolean type;
    • type-range;
    • enumerated type.

    Common features for ordinal types are: each type has a finite number of possible values; the value of these types can be ordered in a certain way and a certain number, which is a serial number, can be associated with each number; adjacent values ​​of ordinal types differ by one.

    For values ​​of an ordinal type, the function ODD(x) can be applied, which returns the ordinal number of the argument x.

    Function PRED(x) - returns the previous value of an ordinal type. PRED(A) = 5.

    SUCC(x) function - returns the next ordinal value. SUCC(A) = 5.

    Character type

    Character type values ​​are 256 characters from the set allowed by the code table of the computer being used. The initial area of ​​this set, that is, the range from 0 to 127 corresponds to the set of ASCII codes, where the alphabet characters, Arabic numbers and special characters are loaded. Symbols initial area are always present on the PC keyboard. The senior region is called the alternate region, it contains characters from national alphabets and various special characters and pseudographics characters that do not correspond to the ASCII code.

    A character type value takes up one byte in RAM. In the program, meanings are enclosed in apostrophes. Values ​​can also be specified in the form of their ASCII code. In this case, you need to put a # sign in front of the number with the symbol code.

    C:= 'A'

    Logical (Boolean) type

    There are two Boolean values: True and False. Variables of this type are specified official word BOOLEAN. Boolean values ​​occupy one byte in RAM. The values ​​True and False correspond to the numeric values ​​1 and 0.

    Type-range

    There is a subset of its base type, which can be any ordinal type. A range type is defined by the boundaries within the base type.

    [minimum-value]…[maximum-value]

    The range type can be specified in the Type section, as a specific type, or directly in the Var section.

    When determining the type range, you must be guided by:

    • the left border should not exceed the right border;
    • a range type inherits all the properties of the base type, but with limitations associated with its lower power.

    Enum type

    This type belongs to ordinal types and is specified by enumerating the values ​​that it can enumerate. Each value is called a certain identifier and is located in the list, framed in parentheses. The enumerated type is specified in Type:

    Peoples = (men, women);

    The first value is 0, the second value is 1, etc.

    Maximum power 65535 values.

    String type

    The string type belongs to the group of structured types and consists of the base type Char. The string type is not an ordinal type. It defines many character strings of arbitrary length up to 255 characters.

    In a program, a string type is declared with the word String. Since String is a base type, it is defined in the language and declared variable type String is implemented in Var. When declaring a variable of string type as String, it is advisable to indicate the length of the string in square brackets. An integer from 0 to 255 is used to indicate.

    Fam: String;

    Specifying the string length allows the compiler to allocate the specified number of bytes in RAM for this variable. If the length of the string is not specified, then the compiler will allocate the maximum possible number of bytes (255) for the value of this variable.