UnitProgramHeader: Uses (the clause lists the units linked to the Program), block (Declaration, command statement)
A program includes:
• A program header (Program heading)
• A uses clause (optional), and
• A block containing declarations and command statements)
A unit starts with the unit heading, followed by interface, implementation, and initialization.
And finalization. The latter two parts are optional.
A uses clause can appear in:
• Project files of programs or libraries
• Unit Interface section, and
• Unit implementation
In the unit declaration and uses clause (especially in Linux), the unit name must be in the same case as the file name. In other cases
For example, the unit name is case-insensitive. To avoid problems in unit reference, you must specify the unit file:
Uses myunit in "myunit. Pas ";
If a unit is explicitly referenced in the project file as above, it can be referenced in other source files simply as below,
Case-insensitive:
Uses myunit;
The uses clause provides the compiler with dependency information between modules. because the information is stored in the module itself, Object Pascal
The program does not need makefile files, header files, and include pre-processing commands (you may need to refer to the C language for these concepts ). Every time
When a project is transferred to the IDE, project manager creates a makefile, but only saves the files in the project group that contains multiple projects.
.
• Unit source file (. Pas)
• Engineering file (. DPR)
• Package source file (. dpk)
• Form file with the extension:. DFM (Delphi) or. XFM (kylix)
• Resource file with the extension of. Res (compiled resource file)
• Project option file with the extension:. DOF (Delphi) or. KOF (kylix)
To change the program icon, use the Project Options dialog box.
<Leo> right-click the Form Designer and the pop-up popmenu has a view as text item to view the association between events and processes.
The event handle is assigned to the specified event through a Form file (. DFM in windows and. XFM in Linux ).
Space (#32) and control character (#0 to #31, where #13 is the carriage return or line terminator) are called blank characters (blank ).
Special symbols are non-literal and numeric characters, or a combination of these characters, they have a fixed meaning.
'[' Equivalent '(. ','] 'is equivalent '.) ';' (* 'and' *) 'are equivalent to' {'and'} '(indicating comment) respectively ).
,! (Exclamation point), "(double quotation marks), % (percent ),? (Question mark),/(backslash), _ (underline), | (channel), and ~ (Broken down
Is not a special symbol
$ Prefix indicates a hexadecimal number, for example, $ 8f.
A tag is an Arabic sequence of no more than four digits, that is, from 0 to 9999. it is meaningless to start with 0.
. If there is no content ('') in single quotes, it is called a null string ). In a reference string, two consecutive single quotes ('') indicate a word.
Character, that is, the single quotation mark itself (')
A control string consists of one or more control characters (controllers). Each control string contains one # followed by an unsigned integer (10
Hexadecimal or hexadecimal). The integer ranges from 0 to 255, indicating the corresponding ASCII characters. The following control string
#89 #111 #117
It is equivalent to a reference string.
'You'
The operators include: @ not ^ */Div mod and shl shr as +-or XOR = ><><=> = in and is.
The behavior of some operators varies according to the data types passed to them. For example, when the not operator is used as an integer
BITs are used for Inverse operations, while boolean values are not used for logical operations.
Except ^, is, and in, other operations can be applied to the variant type.
• No matter what the types of X and Y are, the results of x/y are always extended. For other operators, there is only one
The operation number is a real number, and the result is an extension type. In addition, if one operation number is an int64 type, the result is an int64 type. Otherwise
The result is an integer. If an operation number is a subinterface of an integer, it is treated as an integer.
• The value of X Div y is an integer, that is, the value of x/y is obtained, and the nearest integer is obtained in the direction of 0.
• Mod operation returns the remainder of the number after integer division. In other words, X mod y = x-(x Div y) * Y.
Boolean operators not, And, or, and XOR act on the number of Boolean operations and return a Boolean value.
Use the $ B compiler to control the computation mode. The default status is {$ B-}, which adopts partial computation. Complete partial Calculation
InCodeUse the {$ B ++} sign. You can also select complete Boolean In the Compiler Options dialog box.
Evaluation option. Full calculation is used throughout the project.
<Leo> // partially calculate equals short-circuit computation in C ++/C #
If any number of operations is of the variant type, the compiler always performs full calculation (even in the {$ B-} State)
Logical (bitwise) Operators (bit logical operator): Not and or XOR SHL SHR
• The return type and operation count of bitwise inverse (not) operations are the same;
• If and, or are all integer types, its return type is containing all possible values of the number, and the minimum range is predefined (built-in
) Integer type;
• X shl y and x shr y shift the value of X to the left or right, which is equivalent to X multiplication or division by 2 ^ y (the power of Y in 2). Return type
Same as X. For example, if n is 01101 (13 in decimal format), then n SHL 1 returns 11010 (26 in decimal format ). Note that the value of Y is interpreted
Perform a modulo operation on the type (number of digits) of X. For example, if X is an integer, x shl 40 is interpreted as x shl 8 because integer
The size is 32 bits (4 bytes), 40 mod 32 is equal to 8.
Relational operators =, <>, <,>, <=, and> = can both operate on strings and use operators to connect two strings.
• The number of operators can be a string, packed string (packed arrays of type char), or character. However, if one of the operations is
It is a wide character (widechar) type, and the number of other operations must be a long string.
• The return result of the + operator is compatible with any string type. However, if the number of operations is a short string or character, and their combination length is greater than 255
, The first 255 characters are returned.
Relational operators <,>, <=, and> = can operate on the pchar type. Other operators: +-add or subtract the pointer ^ = <>
Equal to not equal
For the pointer type, type conversion must be performed before dereference.
• If I is an integer and P is a character pointer, P + I adds the address of P to I, that is, a pointer to the end of P.
The pointer at the I character (expression I + P is equivalent to P + I); p-I is to subtract I from the P address, that is, a pointer to P
The pointer at the first I character.
• If both p and q are character pointers, p-Q calculates the difference between P addresses (high addresses) and Q addresses (low addresses ).
Returns an integer that represents the number of characters between p and q. P + q is meaningless.
Set operators (set operators ):
+ Union-difference set * intersection <= less than or equal to (subset)> = greater than or equal to (superset) = equal to <> not equal to in member relationship
The following rules apply to the +,-, and * operators:
• When and only when the ordinal number (a value in the basic set type) O belongs to the set X or the set Y (or both X and Y,
O belongs to X + Y. if and only when o belongs to X but not y, O belongs to x-y. if and only when o belongs to both X and Y
O belongs to x * Y.
• The +,-, and * calculation results belong to the set type set of A. B. Here, a is the minimum ordinal number in the result, and B is the result
Maximum ordinal number.
The following rules apply to the <=, >=, =, <>, and in operators:
• X <= y is true only when every member of X (SET) is a member of Y (SET). z> = W is equivalent
W <= z; u = V is true only when u (SET) and V (SET) share the same members. Otherwise, u <> V
True;
• For ordinal O and set S, O in S is true only when o is a member of S.
• Apart from the comparison of real numbers and integers, the two calculation numbers must be compatible;
• The character string is compared based on the order of each character in the extended ASCII character set. The character type is treated as the length
A string of 1;
• To compare two packed strings, they must have the same number of elements; a packed string with n elements
When compared with a string, it is considered as a string with a length of N;
• The <,>, <=, and> = operators can only be used when both pchar pointers point to the same character array.
For them;
• Operators = and <> can use the class or class reference type as the number of operations. When used for the class type, the calculation rules of = and <> and
The pointer is the same. Only when C and D point to the same instance object, c = D is true; otherwise, C <> D is true.
If C and D indicate the same class, c = D is true. Otherwise, C <> D is true.
The AS and is operators use classes and objects (instances) as operation numbers, and the as operator is also used for interface types.
@ Operator returns the address of a variable, function, process, or method. That is to say, @ operator constructs
Pointer.
• If X is a variable, @ x returns the address of X (there are special rules when X is a process-type variable. For more information, see
Statement and the process type in the expression ). If the default compiler indicator {$ T-} is in effect, the @ X type is
Pointer (Universal pointer); In the {$ t +} state, the @ X type is ^ t, where T is the X type;
• If F is a routine (a function or process), @ F returns the entry point of F, and the type of @ F is always pointer;
• When @ acts on the class method, you must use the class name to limit the method name. For example
@ Tmyclass. dosomething
It points to the dosomething method of tmyclass.
Set Constructors)
[5, 6, 7, 8]
[5 .. 8]
[Red, green, mycolor]
[1, 5, 10 .. k mod 12, 23]
['A'... 'Z', 'A'... 'Z', CHR (digit + 48)]
Set constructor [] indicates an empty set
Strings, arrays, array attributes, and pointers to strings or arrays can be indexed. For example: filename [21]
The syntax for force type conversion is
Typeidentifier (expression)
If the expression is a variable, the result is called variable typecast (variable conversion); otherwise, the result is a value typecast (value conversion ). Although
However, they have the same syntax, but they have different conversion rules.
In value conversion, the type specifiers and conversion expressions must be of an ordered type or pointer type. Examples of value conversion include:
INTEGER ('A ')
Char (48)
Boolean (0)
Color (2)
Longint (@ buffer)
The Declaration syntax and position depend on the type of the identifier to be defined. Generally, a statement can only appear at the beginning of a block,
And the start of the unit interface or implementation part (after the uses clause ).
The hint indicates that platform, deprecated, and library can be attached after any declaration. When declaring a process or function
Use a semicolon to separate the hint and other parts of the statement. For example:
Procedure someoldroutine; stdcall; deprecated;
VaR versionnumber: real library;
Type apperror = Class (exception)
...
End platform;
WhenSource codeWhen compiling in the {$ hints on} {$ warnings on} status
An appropriate prompt or warning will be generated. Use platform to mark an entry and a specific operating system (such as windows and
Linux); deprecated indicates that the entry has been discarded or supported only for backward compatibility; library indicates that the entry depends on
A specific library or component framework (such as VCL or clx ).
The format of the value assignment statement is as follows:
Variable: = expression
Here, variable is any variable reference, including variable, variable type conversion, unreferenced pointer, or a group of structure variables.
Expression is a value-assignment-compatible expression. (In the function block, variable can be replaced by the function name. For more information, see procedures.
And functions.
When you enable the extended syntax ({$ x +}), you can call a function in the same way as the call procedure. When you call a function in this way, its return value is ignored.
Goto label
Label: Statement
Label label;
Label label1,..., labeln;
• Compound or with statements simply execute a series of statements;
• A Condition Statement, that is, an if or case statement, can execute a maximum of one branch according to the specified criteria;
• Loop statements, including repeat, while, and for loops, repeatedly execute a series of statements;
• A set of special statements, including the raise, try... try t, and try... finally structures, used to create and handle exceptions.
The syntax of the with statement is
With OBJ do statement
Or
With obj1,..., objn do statement
The IF statement has two forms: If... then and if... then... else.
For example,
If J = 0 then
Exit
Else
Result: = I/J;
If j <> 0 then
Begin
Result: = I/J;
Count: = count + 1;
End
Else if Count = last then
Done: = true
Else
Exit;
Case statements (case statement ):
Case selectorexpression
Caselist1: statement1;
...
Caselistn: statementn;
Else
Statements;
End
Case I
1. 5: caption: = 'low ';
6. 9: caption: = 'high ';
0, 10 .. 99: caption: = 'out of range ';
Else
Caption: = '';
End;
Selectorexpression is any sorted expression (the string is invalid), which is the same as C ++.
. Object Pascal has three types of loops: repeat statements, while statements, and for statements.
Use the break and continue processes to control the repeat, while, or for statements.
The repeat statement syntax is
Repeat statement1;...; statementn; until expression
For example:
Repeat
Write ('enter a value (0 .. 9 ):');
Readln (I );
Until (I> = 0) and (I <= 9 );
The while statement syntax is
While expression do statement
For example:
While I> 0 do
Begin
If Odd (I) then z: = z * X;
I: = I Div 2;
X: = sqr (X );
End;
The syntax of the for statement is
For counter: = initialvalue to finalvalue do statement
Or
For counter: = initialvalue downto finalvalue do statement
For example: for C: = red to blue do check (C );
A block contains a series of statements followed by a statement. All declarations must appear at the beginning of the block.
The format is
Declarations;
Begin
Statements;
End;
For example:
Function uppercase (const S: string): string;
VaR
Ch: Char;
L: integer;
Source, DEST: pchar;
Begin
...
End;
Types can be divided into basic (Fundamental) and general (generic) types. The range and format of the basic type are the same, no matter what type of CPU and
Operating System. The range and format of general types are platform-related.
Most built-in types belong to the basic type, but a few integers, characters, strings, and pointers belong to the general type.
When needed, using common data types is a good concern because they provide optimized performance and ease of use.
Types can also be classified into simple type, string type, structure type, pointer type, process type, and variant type.
The following outline shows the Object Pascal data type classification:
Simple
Ordinal ordered type
Integer generally (generic) Integer type is integer and Cardinal
The basic integer types include struct int, smallint, longint,
Int64, byte, word, and longword
The basic character types of character are ansichar and widechar.
The general character type is Char, which is equivalent to ansichar.
Boolean: Boolean, bytebool, wordbool, and longbool. boolean is the first choice.
Enumerated
Subrange
Real real number real48 single double extended comp currency
String string type comment string ansistring widestring
Structured
Set
Array
Record
File
Class
Class reference
Interface
Pointer
Procedural
Variant
(Type identifier)
The standard function sizeof acts on all variables and type identifiers. It returns an integer that indicates
Memory size (in bytes ).
Ordered types include integer, character, Boolean, enumeration, and subfield type. An ordered type defines a set of ordered values, except for its
Each other value has a unique predecessor. Each value has a unique predecessor.
Unique successor value (successor)
Ord ordered type expression ordinal value
PRED value of the regular expression
The successor value of the succ ordered expression.
The maximum value of a variable or flag type of the high ordered type
Minimum value of a variable or flag type of the low ordered type
Routines such as high, low, succ, Pred, Inc, Dec, inttostr, and inttohex fully support the int64 parameter.
In addition, round, trunc, strtoint64, and strtoint64def return int64 values;
A few routines (including ORD) cannot operate int64 values at all.
When the last value of the integer type is increased or the first value is reduced, the result is returned to the beginning or end of the integer type.
. The built-in function CHR returns a character value of any integer within the range of ansichar or widechar. For example, CHR (65) returns the letter.
<Leo> what is Inc?
For bytebool, wordbool, and longbool, if its ordinal number is not 0, it is considered true. In an environment that requires the boolean type,
The compiler automatically converts a non-0 value to true.
In Object Pascal, a Boolean expression cannot be converted to an integer or a real number. Therefore, if X is an integer variable, the statement
If X then ...;
Compilation errors may occur.
To define an enumeration type, use the following syntax:
Type typename = (val1,..., valn)
Type size = (small = 5, medium = 10, large = Small + medium );
For example, type suit = (Club, diamond, heart, spade );
When declaring a variable, you can directly use (val1 ,..., Valn) structure, which is like a type name:
VaR MyCard: (Club, diamond, heart, spade );
The subinterface type represents a subset of other ordered types (called Basic Types). Its form is low... high.
• The extended type has higher precision than other real types, but is not lightweight enough. When files created using the extended type need to be split
Be careful when sharing instances.
• The computational type is optimized for Intel CPU, expressed as a 64-bit integer, but is classified as a real number because
The behavior for it is not as sequential (for example, you cannot increment or decrease ). Retain this type only for backward compatibility, using int64
To achieve better performance.
• The currency type has a fixed decimal point, which can minimize errors in currency operations. It is stored as a scaled
64-bit integer with the four least significant digits implicitly representing decimal places.
When a statement or expression is used in combination with other real numbers, its value is automatically divided by or multiplied by 10000.
In the value assignment statement and expression, the string type can be used together, And the compiler automatically performs the necessary conversion. But for procedures or functions,
The type must be correct when the string is passed using the reference method.
In the default {$ H +} status, the compiler interprets string (when it is not followed by braces containing digits) as ansistring;
Use the {$ H-} indicator to interpret it as a string.
The standard function length returns the number of characters in a string. setlength specifies the length of a string.
Indexes can be used for strings like arrays. If S is a string variable and I is an integer expression, s [I] indicates
I character (or, strictly speaking, the first byte ). For values string or ansistring, s [I] is of the ansichar type;
Widestring, s [I] is of the widechar type. Statement mystring [2]: = 'a'; assign value A to the 2nd characters of mystring.
If S is a struct string variable, ord (s [0]), like length (s), returns the length of S. assign a value to s [0], just like calling setlength
, Will change the length of S.
VaR mystring: String [100]; declares a variable named mystring. Its maximum length is 100 bytes,
Standard Functions high and low can act on Short-string type names and variables. High returns its maximum length, and low returns 0.
The ansistring type is also called a long string. It can be dynamically allocated and its length is limited only by memory. It uses 8-bit ANSI characters.
A long string variable is a pointer that occupies 4 bytes of memory. When the variable is null (that is, a string with a length of 0), the pointer is nil,
At this time, it does not need additional memory
Because long string variables are pointers, two or more variables can reference the same value without additional memory. Compiler benefits
Use this to save resources and quickly assign values. As long as a long string variable is destroyed or assigned a new value, the original string (before the variable
Value) the reference count minus 1, and the new value (if any) the reference count plus 1. If the reference count is 0, its memory is released. This
The process is called reference-counting. When you use a string index to change one of the characters, if the string reference count is greater than 1,
Generate a copy of the string, which is called the copy-on-write mechanism.
The widestring type is a dynamically assigned string consisting of 16 Unicode characters. In most aspects, IT and ansistring
Similar. (Note: The wide string does not support reference counting and does not support the copy-on-write mechanism, but supports dynamic memory allocation .)
In Win32, The widestring and com bstr types are compatible.
Object Pascal supports single-byte, multi-byte characters, and strings. applicable types include char, pchar, ansichar, and pansichar.
And ansistring. Using an index on a multi-byte string is not desirable, because s [I] indicates the I-th byte in S (not necessarily the I-th character
But, the standard string processing functions have multi-byte versions, they also implement locale-specific ordering for characters (multiple
The name of a byte function usually starts with ANSI. For example, the multi-byte version of strpos is ansistrpos ). Multi-byte support depends on the Operating System
And local settings (current locale ).
Object Pascal uses the widechar, pwidechar, and widestring types to support Unicode characters and strings.
String constants are compatible with pchar and pwidechar, and the latter two indicate pointers,
They point to an array of char or widechar characters ending with 0.
A character pointer (pchar or pwidechar) can use indexes like an array.
The structure types include set, array, record, class-reference, and interface type.
Except that the set can only contain ordered values, the structure type can contain other structure types, and the level of the structure is not limited.
By default, the value of a structure type is rounded to word or double-word, which makes access faster.
When declaring a structure type, it can contain the keyword packed, which will compress the data storage (not compression, but no longer
Round the data and keep its natural structure ). For example:
Type tnumbers = packed array [1 .. 100] of real;
Using packed slows down data access and affects type compatibility when character arrays are used.
A set is a collection of values of the same ordered type. The values contained in a set do not have an internal order, and a value is contained twice in the set.
Actual Meaning. Statement:
Type
Tsomeints = 1 .. 250;
Tintset = set of tsomeints;
This is equivalent to: Type tintset = set of 1 .. 250;
With the preceding statement, you can construct a set as follows:
VaR set1, set2: tintset;
...
Set1: = [1, 3, 5, 7, 9];
Set2: = [2, 4, 6, 8, 10]
You can also directly use set... Construct a directly declared variable:
VaR myset: Set of 'A'... 'Z ';
...
Myset: = ['A', 'B', 'C'];
The format Declaration of the following static array types: array [indextype1,..., indextypen] of basetype
For example: var myarray: array [1 .. 100] of char;
A multi-dimensional array is an array, for example:
Type tmatrix = array [1 .. 10] of array [1 .. 50] of real;
It is equivalent
Type tmatrix = array [1 .. 10, 1 .. 50] of real;
You can use an index like mymatrix [] or mymatrix [2] [45].
Packed array [Boolean, 1 .. 10, tshoesize] of integer; equivalent
Packed array [Boolean] of packed array [1 .. 10] of packed array [tshoesize] of integer;
The standard functions low and high act on the array type (the identifier) or variables. They return the minimum value of the array's 1st indexes (types ).
Value and maximum value; length returns the number of elements in the array 1st dimension.
A one-dimensional, compressed (packed), char-Type Static array is called packed string, it is compatible with the string type, and other
The packed string with the same number of elements is compatible.
Array [0. x] of char type array, which is an array of characters starting with 0 subscript. It is used to store zero-ending strings and is similar to the pchar class.
Type compatibility.
When a value is set to a dynamic data group or sent to a setlength function, its memory is reassigned.
Declaration in the form of dynamic array: array of basetype
For example, VAR myflexiblearray: array of real;
To cancel the allocation of dynamic arrays, assign nil to its variables, or pass the variables to finalize.
Do not use the '^' operator for a dynamic array variable, or use the new or dispose process for it.
If X and Y are dynamic array variables of the same type, X: = y causes X to point to the same array as Y (you do not need to assign x
Allocate memory ). Unlike string and static arrays, dynamic arrays are not automatically copied before being written.
Using indexes to assign values to dynamic arrays (for example, myflexiblearray [2]: = 7) does not re-allocate memory to the array; during compilation, the index
The boundary check does not prompt.
To truncate a dynamic array, pass it to setlength or copy, and assign the returned value to the array variable (setlength is usually faster)
A record (similar to a structure in other languages) represents a set of different types of elements. Each element is called a "field". When a record type is declared
You must specify the name and type for each field. The statement record syntax is
Type recordtypename = record
Fieldlist1: type1;
...
Fieldlistn: typen;
End
For example:
Type
Tdaterec = record
Year: integer;
Month: (Jan, Feb, MAR, APR, May, Jun, Jul, Aug, SEP, Oct, Nov, DEC );
Day: 1 .. 31;
End;
It is allocated only when it is instantiated, as shown below:
VaR record1, record2: tdaterec;
Record2: = record1; copy the record1 value to record2
You can also use the record... structure to directly declare the variable:
VaR S: Record
Name: string;
Age: integer;
End;
A record type can have a variant part, which looks like a case statement
Type
Temployee = record
Firstname, lastname: String [40];
Birthdate: tdate;
Case salaried: Boolean
True: (annualsalary: currency );
False: (hourlywage: currency );
End;
To declare a file type, use the following syntax: Type filetypename = file of Type