Phones 

What data types are used in the Pascal language. Ordinal data types in Pascal. Enumerated data type

Ordinal types include (see Figure 4.1) integer, logical, character, enumerated and range types. The ORD(X) function is applicable to any of them, which returns the ordinal number of the value of the expression X. For integer types, the ORD(X) function returns the value of X itself, i.e. ORD(X) = X for X belonging to any shell type. Applying ORD(X) to boolean, character, and enumeration types produces a positive integer in the range 0 to 1 (boolean), 0 to 155 (character), 0 to 65535 (enumeration). A range type retains all the properties of the underlying ordinal type, so the result of applying the ORD(X) function to it depends on the properties of that type.

You can also apply functions to ordinal types:

PRED (X) - returns the previous value of the ordinal type (the value that corresponds to the ordinal number ORD(X) - 1), i.e.

ORD(PRED(X)) = ORD(X) - 1;

SUCC (X) - returns the next ordinal value that matches the ordinal number ORD(X) +1, i.e.

ORD(SUCC(X)) = ORD(X) + 1.

For example, if a program defines a variable

then the PRED(C) function will return the value "4", and the SUCC(C) function will return the value "6".

If we imagine any ordinal type as an ordered set of values, increasing from left to right and occupying a certain segment on the number axis, then the function PRED(X) is not defined for the left, and SUCC(X) for the right end of this segment.

Whole types. The range of possible values ​​of integer types depends on their internal representation, which can be one, two, or four bytes. In table 4.1 shows the name of integer types, the length of their internal representation in bytes and the range of possible values.

Table 4.1

When using procedures and functions with integer parameters, you should be guided by the “nesting” of types, i.e. wherever WORD can be used, BYTE can be used (but not vice versa), LONGINT “includes” INTEGER, which, in turn, includes SHORTINT.

The list of procedures and functions applicable to integer types is given in Table 4.2. Letters b, s, w, i, l Expressions of type BYTE, SHORTINT, WORD, INTEGER and LONGINT are designated respectively, x is an expression of any of these types; letters vb, vs, vw, vi, vl, vx denote variables of the corresponding types. An optional parameter is indicated in square brackets.

Table 4.2

Standard procedures and functions applicable to entire types
Appeal Result type Action
abs(x) x Returns module x
chr(b) Char Returns a character by its code
dec(vx[, i]) - Decreases the value of vx by i, and in the absence of i - by 1
inc(vx[, i]) - Increases the value of vx by i, and in the absence of i - by 1
Hi(i) Byte Returns the high byte of the argument
Hi(w) Same Same
Lo(i) " Returns the low byte of the argument
Lo(w) " Same
odd(l) Boolean Returns True if the argument is an odd number
Random (w) Same as parameter Returns a pseudorandom number uniformly distributed in the range 0...(w-l)
sgr(x) X Returns the square of the argument
swap(i) Integer Swaps bytes in a word
swap(w) Word

When operating with integers, the type of the result will correspond to the type of the operands, and if the operands are of different integer types, the type of the operand that has the maximum power (maximum range of values). Possible overflow of the result is not controlled in any way, which can lead to misunderstandings, for example:

a:= 32767; (Maximum possible INTEGER value)

x:= a + 2; (Overflow while evaluating this expression !}

y:= LongInt(a)+2; (No overflow after casting the variable to a more powerful type)

WriteLn(x:10:0, y:10:0)

As a result of running the program we get

Boolean type. Boolean values ​​can be one of the pre-declared constants FALSE or TRUE. The rules apply to them:

False< True;

succ(False)= True;

pred(True) = False.

Since the Boolean type is an ordinal type, it can be used in a countable type operator, for example:

for 1:= False to True do ....

Character type. 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 the code for the internal representation of the symbol; it is returned by the ORD function.

ASCII code is used for encoding ( American Standard Code for Information Interchange- American Standard Code for Information Interchange). This is a 7-bit code, i.e. it can only encode 128 characters in the range from 0 to 127. At the same time, in the 8-bit byte allocated for storing a character in Turbo Pascal, you can encode twice as many characters in the range from 0 to 255. The first half of the characters PC with codes 0...127 corresponds to the ASCII standard (Table 4.3). The second half of the characters with codes 128...255 are not limited by the rigid framework of the standard and can be changed on different types of PCs (Appendix 2 shows some common encoding options for these characters).

Table 4.3

Character encoding according to the ASCII standard
Code Symbol Code Symbol Code Symbol Code Symbol
NUL B.L. ® "
ZON ! A a
STX " IN b
ETX # WITH With
EOT $ D d
ENQ % E e
ASK & F f
BEL " G g
B.S. ( H h
NT ) I i
LF * J j
VT + k k
FF , L i
CR - M m
SO . N n
S.I. / ABOUT
DEL p P
DC1 Q q
DC2 R r
DC3 S s
DC4 T t
N.A.K. U u
SYN V V
ETB w w
CAN X X
E.M. U U
SUB : z z
ESC / [ {
FS < \ l
G.S. = ] }
R.S. > ^ ~
US ? - n

Characters with codes 0...31 refer to service codes. If these codes are used in the program's character text, they are considered whitespace. When used in I/O operations, they can have the following independent meaning:

Symbol Code Meaning
BEL Call; The display of this symbol is accompanied by sound signal
NT Horizontal tabulation; when displayed on the screen, moves the cursor to a position that is a multiple of 8 plus 1 (9, 17, 25, etc.)
LF Line feed; when displaying it on the screen, all subsequent characters will be output starting from the same position, but on the next line
VT Vertical tab; when displayed on the screen, it is replaced with a special character
FF Page run; when output to a printer, it forms a page; when output to the screen, it is replaced with a special character
CR Carriage return; entered by pressing the Enter key (when entered using READ or READLN, it means the “Enter” command and is not placed in the input buffer; when output, it means the “Continue output from the beginning of the current line” command)
SUB End of file; entered from the keyboard by pressing Ctrl-Z; when output it is replaced with a special sign
SSC End of work; entered from the keyboard by pressing the ESC key; when output it is replaced with a special sign

Relational operations, as well as built-in functions, are applicable to the CHAR type: СНR(В) - function of the CHAR type; converts an expression B of type BYTE into a character and returns it with its value;

UPCASE(CH) - CHAR type function; returns the uppercase letter if CH is a lowercase Latin letter, otherwise returns the CH character itself, for example:

cl:= UpCase("s") ;

c2:= UpCase ("Ф") ;

WriteLn(cl," ",c2)

Since the UPCASE function does not process Cyrillic, the result of running this

programs will be displayed on the screen

Enum type. An enumerated type is specified by an enumeration of the values ​​it can receive. Each value is named by some identifier and is located in a list surrounded by parentheses, for example:

colors =(red, white, blue);

The use of enumerated types makes programs more visual. If, for example, the program uses data associated with the months of the year, then the following fragment of the program:

TypeMonth=(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);

month: TypeMonth;

if month = Aug then WriteLn("It would be nice to go to the sea!");

It would be, you see, very clear. Alas! In Turbo Pascal you cannot use Cyrillic in identifiers, so we are forced to write like this:

TypeMonth=(jan,feb,mar,may,jun,jul,aug,sep,oct,nov,dec);

month: TypeMonth;

if month = aug then WriteLn("It would be nice to go to the sea!");

The correspondence between the values ​​of an enumerated type and the ordinal numbers of these values ​​is established by the enumeration order: the first value in the list receives the ordinal number 0, the second - 1, etc. The maximum capacity of an enumerated type is 65536 values, so in fact an enumerated type defines a certain subset of the whole WORD type and can be considered as a compact declaration of a group of integer constants with values ​​0, 1, etc.

Using enumerated types increases the reliability of programs by allowing you to control the values ​​that corresponding variables receive. For example, let the following enumerated types be given:

colors = (black, red, white);

ordenal= (one, two, three);

days = (monday, tuesday, Wednesday);

In terms of power and internal representation, all three types are equivalent:

ord(black)=0, ..., ord(white)=2,

ord(one)=0, ...ord(three)=2,

ord(monday)=0, ...ord(Wednesday)=2.

However, if the variables are defined

col:colors; num:ordenal;

then the operators are allowed

num:= succ(two);

day:= pred(tuesday);

but unacceptable

As already mentioned, there is a one-to-one correspondence between the values ​​of an enumerated type and the set of integers, specified by the ORD(X) function. Turbo Pascal also allows the reverse conversion: any expression of type WORD can be converted to a value of an enum type, as long as the value of the integer expression does not exceed the power1™ of the enumeration type. This conversion is achieved by using an automatically declared function with the name of the enumerated type (see section 4.4). For example, for the type declaration discussed above, the following assignments are equivalent:

col:= colors(0);

Of course, assignment

will be unacceptable.

Variables of any enumerated type can be declared without first declaring that type, for example:

col: (black, white, green);

Type-range. A range type is a subset of its base type, which can be any ordinal type except a range type. A range type is defined by the boundaries of its values ​​within the base type:

<мин.знач.>..<макс.знач.>

Here<мин.знач. >- minimum value of the type-range;

<макс.знач.>- its maximum value.

For example:

digit = "0".."9";

The range type does not have to be described in the TYPE section, but can be specified directly when declaring a variable, for example:

Ichr: "A".."Z";.

When determining a range type, you must follow the following rules:

  • two ".." characters are treated as one character, so spaces between them are not allowed;
  • the left border of the range should not exceed its right border. A range type inherits all the properties of its base type, but with the limitations of its lower power. In particular, if a variable is defined

days = (mo,tu,we,th,fr,sa,su);

WeekEnd = sa .. su;

then ORD(W) will return the value 5 while PRED(W) will result in an error.

The Turbo Pascal standard library includes two functions that support working with range types:

HIGH(X) - returns the maximum value of the range type to which the variable X belongs;

LOW(X) - returns the minimum value of the range type.

The following short program will print the line

WriteLn(Low(k),"..",High(k))

The set of integers is infinite, but we can always choose the number of bits to represent any integer that arises when solving a specific problem. The set of real numbers is not only infinite, but also continuous, so no matter how many bits we take, we will inevitably encounter numbers that do not have an exact representation. Floating point numbers are one possible way to represent real numbers, which is a trade-off between precision and range of accepted values.

A floating point number consists of a set of individual digits, conventionally divided into sign, exponent, and mantissa. The exponent and mantissa are integers that, together with the sign, give the following representation of a floating point number:

Mathematically it is written like this:

(-1) s × M × B E, where s is the sign, B is the radix, E is the exponent, and M is the mantissa.

The base determines the digit number system. It has been mathematically proven that floating point numbers with base B=2 (binary representation) are most resistant to rounding errors, therefore in practice only bases 2 and, less commonly, 10 are encountered. For further presentation, we will always assume B=2, and the formula for a number with floating point will look like:

(-1) s × M × 2 E

What is mantissa and order? Mantissa is a fixed-length integer that represents the most significant bits of a real number. Let's say our mantissa consists of three bits (|M|=3). Take, for example, the number “5”, which in the binary system will be equal to 101 2. The most significant bit corresponds to 2 2 =4, the middle bit (which is equal to zero) is 2 1 =2, and the least significant bit is 2 0 =1. Order– this is the power of the base (two) of the highest digit. In our case E=2. It is convenient to write such numbers in the so-called “scientific” standard form, for example “1.01e+2”. It is immediately clear that the mantissa consists of three signs, and the order is two.

Let's say we want to get a fractional number using the same 3 bits of the mantissa. We can do this if we take, say, E=1. Then our number will be equal

1.01e+1 = 1×2 1 +0×2 0 +1×2 -1 =2+0.5=2.5

Obviously, in this way the same number can be represented in different ways. Let's consider an example with the length of the mantissa |M|=4. The number “2” can be represented as follows:

2 = 10 (in binary) = 1.000e+1 = 0.100e+2 = 0.010e+3.

Therefore, already in the very first machines, numbers were represented in the so-called normalized form, when the first bit of the mantissa was always assumed to be equal to one.

This saves one bit (since the implicit one does not need to be stored in memory) and ensures that the number is represented uniquely. In our example, “2” has a single representation (“1.000e+1”), and the mantissa is stored in memory as “000”, because the leading unit is implied implicitly. But a new problem arises in the normalized representation of numbers - it is impossible to represent zero in this form.

  • Data analysis using the Parameter Selection and Solution Search commands
  • Analysis and interpretation of experimental psychological research data.
  • Analysis of source data. Technical standards for city roads.
  • ANALYSIS OF OBTAINED DATA. MAKING A DECISION ABOUT THE SUFFICIENCY OR INSUFFICIENCY OF WATER SUPPLY CHARACTERISTICS FOR THE NEEDS OF THE IRRIGATION SYSTEM.
  • Communication line equipment: data transmission equipment, terminal equipment, intermediate equipment.

  • Programming Basics
    Every professional was once a teapot. Surely you are familiar with the state when “you don’t know how to start thinking in order to come up with such a thing.” You've probably encountered a situation where you just don't know where to start. This book is aimed precisely at such people who would like to become a programmer, but have absolutely no idea how to start this path. ...

    Almost all integer data types are . These data types represent integers within a certain range. The exact names of integer types and ranges of values ​​depend on the specific programming language, the compiler, and the compilation mode. You can learn more about this in the compiler documentation.

    For example, data type Integer in Delphi it has a range of -2147483648…2147483647, while in Turbo Pascal the data type Integer represents numbers in the range -35768…32767. In Free Pascal, the range of values ​​of type Integer determined by the selected mode.

    Since Lazarus uses the Free Pascal compiler, everything said about data types in relation to Free Pascal is also true for Lazarus.

    So, Free Pascal's integer data types are listed in Table 13.1.

    Table 13.1. Free Pascal Integer Data Types (Lazarus).

    Type Size, bytes Range of values
    Byte 1 0…255
    Shortint 1 -128…127
    Smallint 2 -35768…32767
    Word 2 0…65535
    Integer 2 or 4 Depends on compilation mode
    Cardinal 4 0…4294967295
    Longint 4 -2147483648…2147483647
    Longword 4 0...4294967295
    Int64 8 -9223372036854775808...9223372036854775807
    QWord 8 0...18446744073709551615

    NOTE
    Types in Free Pascal Int64 And QWord are not! This means that you cannot use them, for example, for index variables in loops. However, I presented them here so as not to describe them separately in the future and to collect all Free Pascal integer types in one place. If you don’t understand some words, don’t be alarmed. In due time I will tell you about everything in more detail.

    And now a few explanations about the table.

    In the column TYPE data type identifiers are given (keywords that indicate to the compiler what type a particular data belongs to). You will learn how to use these identifiers in the following lessons.

    In the column SIZE indicates the size that the data type occupies in computer memory. For example, a positive integer can be represented different types: Byte, Word, Cardinal etc. However, a number like Cardinal will occupy 4 bytes in memory, while a number like Byte– only 1 byte. Therefore, if you know for sure that the number you are working with will never take a value greater than 255, then it is better to define it as a type Byte, as this will save space in your computer's memory. Although not everything is so simple here (the nuances of the distribution of memory and other computer resources are beyond the scope).

    In the column RANGE specifies the range of values ​​that the data type operates on. For example, a number like Byte can take values ​​from 0 to 255.

    Now for practice. Let's write a program that displays the ranges of values ​​of all integer data types. The source code for this program is given below:

    Listing 13.1. A program for displaying ranges of integers. program td; ($mode objfpc)($H+) uses ($IFDEF UNIX)($IFDEF UseCThreads) cthreads, ($ENDIF)($ENDIF) Classes ( you can add units after this ); begin Writeln("Byte: ", Low(Byte), "..", High(Byte)); Writeln("Shortint: ", Low(Shortint), "..", High(Shortint)); Writeln("Smallint: ", Low(Smallint), "..", High(Smallint)); Writeln("Word: ", Low(Word), "..", High(Word)); Writeln("Integer: ", Low(Integer), "..", High(Integer)); Writeln("Cardinal: ", Low(Cardinal), "..", High(Cardinal)); Writeln("Longint: ", Low(Longint), "..", High(Longint)); Writeln("Longword: ", Low(Longword), "..", High(Longword)); Writeln("Int64: ", Low(Int64), "..", High(Int64)); Writeln("QWord: ", Low(QWord), "..", High(QWord)); Readln; end.

    Standard function Low defines the minimum value of the data type. Funtskia High defines the maximum value. With functions WriteLn And ReadLn you already know each other a little. We will talk in more detail about subroutines (procedures and functions) in the corresponding section.

    Finally, I’ll tell you how integer data is written in the program. Yes, just like everywhere else - just write the number, without quotes or any additional symbols. For example, like this

    10
    178
    35278

    True, this applies to numbers in the decimal number system. Surely you already know that there are other systems. The most widely used number systems are binary, decimal and hexadecimal.

    Free Pascal supports four integer formats:

    1. Decimal notation. Just a number, like 10.
    2. Hexadecimal notation. A number prefixed with $. For example, the hexadecimal number $10 is equal to the decimal number 16.
    3. Octal notation. A number prefixed with &. For example, octal &10 is equal to decimal 8.
    4. Binary notation. A number prefixed with %. For example, the binary number %10 is equal to the decimal number 2.

    Homework:

    Create a program that displays ranges of integer values ​​(Listing 13.1). Compile the program and run it. Make sure that these values ​​correspond to those shown in Table 13.1.

    In the source code of the program, find the line that sets the compilation mode:

    ($mode objfpc)($H+)

    In this line, instead of the word objfpc write the word tp. That is, the final line should look like this:

    ($mode tp)($H+)

    Launch the program. Look at the range of type values Integer. Draw conclusions.

    Learn to think like a programmer, that is, logically. No one will chew on everything for you until retirement, as I do now. You have to get used to thinking for yourself. Otherwise, you will slip into the “monkey learning principle,” and then your chances of becoming a great programmer will be close to zero. To help you not slide into the “cramming” level, I will periodically leave gaps in your learning so that you try to figure out some things yourself.

    It's much better if you figure it out yourself wrong decision, you will find the mistake yourself and correct it yourself, rather than always using other people’s correct solutions and stupidly copying them.

    Federal Agency for Education

    Abstract

    "DATA TYPES IN PASCAL"

    1. Data types

    Any data, i.e. constants, variables, properties, function values ​​or expressions are characterized by their types. A type defines the set of valid values ​​that an object can have, as well as the set of valid operations that can be applied to it. In addition, the type also determines the format of the internal representation of data in the PC memory.

    In general, the Object Pascal language is characterized by a branched structure of data types (Fig. 1.1). The language provides a mechanism for creating new types, so the total number of types used in a program can be as large as desired.

    The data processed in the program is divided into variables, constants and literals:

    Constants represent data whose values ​​are set in the constant declaration section and do not change during program execution.

    Variables are declared in the variable declaration section, but unlike constants, they receive their values ​​during program execution, and these values ​​can be changed. Constants and variables can be referred to by name.

    Literal has no identifier and is represented directly by the value in the program text.

    Type defines the set of values ​​that data elements can take and the set of operations allowed on them.

    This and the four subsequent chapters provide detailed descriptions of each type.

    1.1 Simple types

    Simple types include ordinal, real, and datetime types.

    Ordinal types differ in that each of them has a finite number of possible values. These values ​​can be ordered in a certain way (hence the name of the types) and, therefore, each of them can be associated with some integer - the ordinal number of the value.

    Real types, strictly speaking, also have a finite number of values, which is determined by the format of the internal representation of a real number. However, the number of possible values ​​of real types is so large that it is not possible to associate an integer (its number) with each of them.

    Date-time type designed to store date and time. In fact, it uses the real format for these purposes.

    1.1.1 Ordinal types

    Ordinal types include (see Figure 1.1) integer, logical, character, enumerated, and range types. The Ord(x) function can be applied to any of them, which returns the ordinal value of the expression X.


    Rice. 1.1 - Data type structure

    For whole types, the function ord(x) returns the value of x itself, i.e. Ord(X) = x for x belonging to any whole type. Applying Ord(x) to logical , symbolic and enumerable types gives a positive integer in the range 0 to 1 ( boolean type), from 0 to 255 ( symbolic), from 0 to 65535 ( enumerable). Type-range preserves all the properties of the base ordinal type, so the result of applying the ord(x) function to it depends on the properties of this type.

    You can also apply functions to ordinal types:

    pred(x)- returns the previous value of the ordinal type (the value that corresponds to the ordinal number ord(x) -1, i.e. ord(pred(x)) = ord(x) - 1;

    succ(x)- returns the next value of the ordinal type, which corresponds to the ordinal number ord(x) +1, i.e. ord(Succ(x)) = ord(x) + 1.

    For example, if a program defines a variable

    then the PRED(c) function will return the character "4", and the SUCC(c) function will return the character "6".

    If we imagine any ordinal type as an ordered set of values, increasing from left to right and occupying a certain segment on the number axis, then the function pred(x) is not defined for the left, and succ (x) for the right end of this segment.

    Integer types . The range of possible values ​​of integer types depends on their internal representation, which can be one, two, four, or eight bytes. In table 1.1 shows the names of integer types, the length of their internal representation in bytes and the range of possible values.

    Table 1.1 - Integer types

    Name Length, bytes Range of values
    Cardinal 4 0. .. 2 147 483 647
    Byte 1 0...255
    Shortint 1 -128...+127
    Smallint 2 -32 768...+32 767
    Word 2 0...65 535
    Integer 4
    Longint 4 -2 147 483 648...+2 147 483 647
    Int64 8 -9*1018...+9*1018
    LongWord 4 0. . .4 294 967 295

    Types LongWord And Int64 were first introduced in version 4, and the types Smallint And Cardinal not available in Delphi 1. Type integer for this version it takes 2 bytes and has a value range from -32768 to +32767, i.e. the same as Smallint .

    When using procedures and functions with integer parameters, you should be guided by the “nesting” of types, i.e. wherever it can be used word, allowed to use Byte(but not vice versa), in Longint“enters” Smallint, which in turn includes Shortint .

    The list of procedures and functions applicable to integer types is given in table. 1.2. The letters b, s, w, i, l denote expressions of the following type: Byte , Shortint, Word, Integer and Longint ,

    x is an expression of any of these types; the letters vb, vs, vw, vi, vl, vx denote variables of the corresponding types. An optional parameter is indicated in square brackets.

    Table 1.2 - Standard procedures and functions applicable to entire types

    Appeal Result type Action
    abs(x) x Returns module x
    chr(b) Char Returns a character by its code
    dec(vx[,i]) - Decreases the value of vx by i, and in the absence of i - by 1
    inc(vx[,i]) - Increases the value of vx by i, and in the absence of i - by 1
    Hi(w) Byte Returns the highest bow of the argument
    Hi(I) Same Returns the third byte
    Lo(i) Returns the low byte of the argument
    Lo(w) Same
    odd(l) Boolean Returns True if the argument is an odd number
    Random(w) Same as parameter Returns a pseudorandom number uniformly distributed in the range 0...(w-l)
    sqr(x) X Returns the square of the argument
    swap(i) Integer Swaps bytes in a word
    swap(w) Word Same

    When operating with integers, the result type will correspond to the type of the operands, and if the operands are of different integer types, a common type that includes both operands. For example, when operating with shortint And word the common type will be integer. In the default setting, the Delphi compiler does not produce code to check whether a value is out of range, which can lead to misunderstandings.

    Boolean types . Logical types include Boolean, ByteBool, Bool, wordBool And LongBool. In standard Pascal, only the type is defined Boolean, other logical types are introduced into Object Pascal for compatibility with Windows: types Boolean And ByteBool each takes up one byte, Bool And WordBool- 2 bytes each, LongBool- 4 bytes. Boolean values ​​can be one of the pre-declared constants False or True.

    Since the Boolean type is an ordinal type, it can be used in a loop statement of a countable type. In Delphi 32 for Boolean meaning

    Ord (True) = +1, while for other types ( Bool, WordBool etc.)

    Ord(True) = -1, so these kinds of operators should be used with caution! For example, for Delphi 6 version, the executable showMessage(" --- ") statement in the following loop for will never be executed:

    for L:= False to True do

    ShowMessage("--);

    If we replace the loop parameter type L in the previous example with Boolean, the loop will run and the message will appear on the screen twice. [For Delphi versions 1 and 2 ord (True) =+1 for any boolean type.]

    Character type . 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 the code for the internal representation of the symbol; it is returned by the ord function.

    For encoding in Windows, the ANSI code is used (named after the American National Standard Institute, the American standardization institute that proposed this code). The first half of PC characters with codes 0... 127 corresponds to Table 1.3. The second half of characters with codes 128...255 varies for different fonts. Standard Windows fonts Arial Cyr, Courier New Cyr and Times New Roman use the last 64 codes (from 192 to 256) to represent Cyrillic characters (without the letters “ё” and “Ё”): “A”... “Z” are encoded values ​​192..223, “a”... “i” - 224...255. The symbols “Ё” and “е” have codes 168 and 184, respectively.

    Table 1.3 - Character encoding in accordance with the ANSI standard

    Code Symbol Code. Symbol Code. Symbol Code Symbol
    0 NUL 32 B.L. 64 @ 96 "
    1 ZON 33 ! 65 A 97 A
    2 STX 34 66 IN 98 b
    3 ETX 35 # 67 WITH 99 With
    4 EOT 36 $ 68 D 100 d
    5 ENQ 37 % 69 E 101 e
    6 ACK 38 & 70 F 102 f
    7 BEL 39 " 71 G 103 d
    8" B.S. 40 ( 72 N 104 h
    9 HT 41 ) 73 I 105 i
    10 LF 42 * 74 J 106 j
    11 VT 43 + 75 TO 107 k
    12 FF 44 F 76 L 108 1
    13 CR 45 - 77 M 109 m
    14 SO 46 78 N 110 n
    15 S.I. 47 / 79 0 111 O
    16 DEL 48 0 80 R 112 P
    17 DC1 49 1 81 Q 113 q
    18 DC2 50 2 82 R 114 r
    19 DC3 51 3 83 S 115 s
    20 DC 4 52 4 84 T 116 t
    21 N.A.K. 53 5 85 U 117 u
    22 SYN 54 6 86 V 118 v
    23 ETB 55 7 87 W 119 W
    24 CAN 56 8 88 X 120 x
    25 E.M. 57 9 89 Y 121 U
    26 SUB 58 : 90 Z .122 z
    27 ESC 59 ; 91 t 123 {
    28 FS 60 < 92 \ 124 1
    29 G.S. 61 = 93 ] 125 }
    30 R.S. 62 > 94 L 126 ~
    31 US 63 F 95 127 r

    Characters with codes 0...31 refer to service codes. If these codes are used in the program's character text, they are considered whitespace.

    The concept of data is one of the key ones in programming, and in computer science in general. Roughly speaking, data in computer science is information that is in a state of storage, processing or transmission over a certain period of time. In Turing machines, information has a type, and this in turn depends on the type of information.

    Data types in Pascal define the possible values ​​of variables, constants, expressions, and functions. They are built-in and custom. Built-in types are initially present in the programming language, and custom types are created by the programmer.

    According to the method of presentation and processing, data types are:

    • simple
    • structured
    • pointers
    • objects
    • procedures

    This article will consider only the simplest data types, since at the initial stages of training, it will be easier for your program to do without, for example, files and records than without integer or string variables.

    Integer type

    This includes several integer types, which differ in the range of values, the number of bytes allocated for storing them, and the word with which the type is declared.

    Type Range Size in bytes
    shortint -128…127 1
    integer -32 768…32 767 2
    longint -2 147 483 648…2 147 483 647 4
    byte 0…255 1
    word 0…65 535 2

    You can declare an integer variable in the Var section, for example:

    All arithmetic and logical operations can be performed on variables in this category with the exception of division (/), which requires a real type. Some standard functions and procedures may also apply.

    Real type

    In Pascal there are the following real data types:

    Type Range Memory, byte Number of digits
    Real 2.9e-39 … 1.7e38 6 11-12
    Single 1.5e-45 … 3.4e38 4 7-8
    Double 5.0e-324…1.7e308 8 15-16
    Extended 3.4e-4932 … 1.1e493 10 19-20
    Comp -9.2e63…(9.2e63)-1 8 19-20

    More operations and functions can be performed on them than on integers. For example, these functions return a real result:

    sin(x) – sine;

    cos(x) – cosine;

    arctan(x) – arctangent;

    ln(x) – natural logarithm;

    sqrt(x) – square root;

    exp(x) – exponent;

    Boolean type

    A variable of a Boolean data type can take only two values: true and false. Here, true corresponds to the value 1, and false corresponds to zero. You can declare a Boolean variable like this:

    Comparison and logical operations can be performed on data of this type: not, and, or, xor.

    Character type

    A character data type is a collection of characters used in a particular computer. A variable of this type takes the value of one of these characters and occupies 1 byte in computer memory. Word Char defines a value of this type. There are several ways to write a character variable (or constant):

    1. as a single character enclosed in apostrophes: ‘W’, ‘V’, ‘n’;
    2. by specifying the character code, the value of which must be in the range from 0 to 255.
    3. using the ^K construction, where K is the control character code. The value of K must be 64 greater than the corresponding control character code.

    Relational operations and the following functions are applicable to values ​​of a character data type:

    Succ(x)- returns the next character;

    Pred(x)- returns the previous character;

    Ord(x)- returns the value of the character code;

    Chr(x)- returns the value of a symbol by its code;

    UpCase(x)- converts letters from the interval ‘a’..’z’ to upper case.

    To work effectively with the character type, I recommend using .

    String type

    A string in Pascal is a sequence of characters enclosed in apostrophes, and is denoted by the word String. The number of characters (line length) must not exceed 255. If the length of the line is not specified, it will automatically be determined to be 255 characters. The general form of a string variable declaration looks like this:

    Var<имя_переменной>:string[<длина строки>];

    Each character in a line has its own index (number). The index of the first byte is 0, but it does not store the first character, but the length of the entire string, which means that a variable of this type will occupy 1 byte more than the number of variables in it. The number of the first character is 1, for example, if we have the string S=‘stroka’, then S=s;. In one of the following lessons, the string data type will be discussed in more detail.

    Enumerated data type

    An enumerated data type represents a limited number of identifiers. These identifiers are enclosed in parentheses and separated by commas.

    Type Day=(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

    Variable A can only take the values ​​specified in the Type section. You can also declare a variable of enumerated type in the Var section:

    Var A: (Monday, Tuesday);

    TO this type relational operations are applicable, and it is predetermined that Monday

    Interval data type

    When it is necessary to specify a range of values, then in such situations the interval data type is used. The construction used for the declaration is m..n, Where m is the minimum (initial) value, and n– maximum (final); here m and n are constants, which can be of integer, character, enumeration or boolean type. Values ​​of interval type can be described both in the types section and in the variable description section.

    General view:

    TYPE<имя_типа> = <мин. значение>..<макс. значение>;

    When describing a variable, you must indicate its type. The type of a variable describes the set of values ​​it can take and the actions that can be performed on it. A type declaration specifies an identifier that represents the type.

    Simple types are divided into standard (ordinal) and enumerated (restricted).

    Standard types

    Turbo Pascal has four built-in standard types: integer, real, boolean, and char.

    Integer type (integer)

    Turbo Pascal has five built-in integer types: shortint, integer, longint, byte, and word. Each type denotes a specific subset of integers, as shown in the following Table.

    Built-in integer types.

    Range

    Format

    8 signed bits

    16 signed bits

    2147483648 +2147483647

    32 bit signed

    8 bits unsigned

    16 bits unsigned

    Arithmetic operations on operands of integer type are performed in accordance with the following rules:

    1. An integer constant type is a built-in integer type with the smallest range that includes the value of that integer constant.
    2. In the case of a binary operation (an operation that uses two operands), both operands are converted to their common type before the operation is performed on them. The common type is the built-in integer type, with the smallest range that includes all possible values ​​of both types. For example, the common type for an integer and a byte-length integer is integer, and the common type for an integer and a word-length integer is long integer. The action is performed according to the precision of the generic type and the type of the result is the generic type.
    3. The expression on the right of the assignment operator is evaluated regardless of the size of the variable on the left.

    Operations performed on integers:

    “+” - addition

    “-“ - subtraction

    "*" - multiplication

    SQR - squaring

    DIV - discards the fractional part after division

    MOD - obtaining the integer remainder after division

    ABS - number module

    RANDOM(X) - obtaining a random number from 0 to X

    A:=100 ; b:=60 ; a DIV b result - 1 a MOD b result - 40

    Integer type variables are described as follows:

    var variable list: type;

    For example: var а,р,n:integer;

    Real type(real)

    The real type is a subset of real numbers that can be represented in floating point format with a fixed number of digits. Writing a value in floating point format typically involves three values ​​- m, b and e - such that m*b e, where b is always 10 and m and e are integer values ​​in the real range. These values ​​of m and e further determine the range and precision of the real type.

    There are five types of real types: real, single, double, exnende, comp. Real types vary in the range and precision of the values ​​associated with them

    Range and decimal digits for real types

    Range

    Numbers

    2.9x10E-39 to 1.7x10E 38

    1.5x10E-45 to 3.4x10E 38

    5.0x10E-324 to 1.7x10E 308

    3.4x10E-493 to 1.1x10E 403

    2E 63 to 2E 63

    Operations performed on real numbers:

    • All operations are valid for integers.
    • SQRT(x) is the square root of x.
    • SIN(X), COS(X), ARCTAN(X).
    • LN(X) is the natural logarithm.
    • EXP(X) is the exponent of X (e x).
    • EXP(X*LN(A)) - exponentiation (A x).
    • Type conversion functions:
      • TRUNC(X) - discards the fractional part;
      • ROUND(X) - rounding.
    • Some rules of arithmetic operations:
      • If an arithmetic operation contains numbers of type real and integer, then the result will be of type real.
      • All components of the expression are written on one line.
      • Only parentheses are used.
      • You cannot put two arithmetic signs in a row.

    Variables of real type are described as follows:

    var variable list: type;

    For example:

    var d,g,k:real ;

    Character type(char)

    The char type is any character enclosed in apostrophes. To represent an apostrophe as a character variable, you must enclose it in an apostrophe: ''''.

    Each character has its own code and number. The serial numbers of digits 0,1..9 are ordered in ascending order. The serial numbers of the letters are also ordered in ascending order, but do not necessarily follow each other.

    The following comparison signs apply to character data:

    > , < , >=, <=, <> .

    For example: 'A'< ‘W’

    Functions that apply to character variables:

    1. ORD(X) - determines the serial number of the symbol X. ord (‘a’) =97 ;
    2. CHR(X) - identifies a character by number. chr(97) =’a’;
    3. PRED(X) - returns the character preceding the X character. pred (‘B’) =’A’;
    4. SUCC(X) - returns the character following the X character. succ (‘A’) =’B’;

    Enum type

    An enumerated data type is so named because it is specified as a list of constants in a strictly defined order and in a strictly defined quantity. An enumerated type consists of a list of constants. Variables of this type can take the value of any of these constants. The enumeration type description looks like this:

    Type<имя типа>=(list of constants) ; Var<имя переменной>:<имя типа>;

    Where<список констант>- this is a special type of constants, specified separated by commas and having their own serial number, starting from 0.

    For example:

    type direction=(north, south, west, east) ; month=(June, July, August, January) ; capacity=(bucket, barrel, canister, tank) ; var rotation:direction; departure:month; volume:capacity; var turn:(north, south, west, east) ; departure: (June, July, August, January) ; volume: (bucket, barrel, canister, tank);

    You can perform the following assignment operators:

    Rotation:=south; departure:=August; volume:=tank;

    but you can't do mixed assignments:

    Departure:=south; volume:=August;

    The following functions apply to variables of enumerated type:

    1. ORD - serial number

    2. PRED - previous element

    3. SUCC - subsequent element.

    PRED (barrel) = bucket; SUCC (south) =west; ORD (July) =1 ;

    Variables of an enumerated type can be compared because they are ordered and numbered. So the expressions: north< юг, июнь < январь имеют значения TRUE, а юг>west and tank<бочка значение FАLSE.

    Limited type

    If a variable does not accept all values ​​of its type, but only within a certain range, then it can be considered a variable of a limited type. Each constrained type is defined by imposing a constraint on the base types.

    It is described as follows:

    TYPE<имя типа>=constant1 ..constant2

    In this case, the following rules must be followed:

    1. Both bounded constants must be of the same type.
    2. Any simple type except real can be used as a base type.
    3. The initial value when defining a limited type must not be greater than the final value.
    type index =0 ..63 ; letter=’a’..’z’; var char1,char2:letter; a,g:index ;

    You can describe it immediately in the variable description section:

    var a,g:0 ..63 ; char1,char2:'a'..'z'.