1.6 Object Pascal Operator
Operators are symbols used to calculate various types of data in program code. They are generally classified into Arithmetic Operators, logical operators, comparison operators, and bitwise operators.
1. Arithmetic Operators
Arithmetic Operators of Object Pascal, as shown in table 1-9.
Table 1-9 Object Pascal Language Arithmetic Operators
| Operator |
Operation |
Operation Data Type |
Result type |
| + |
Add |
Integer and real type |
Integer and real type |
| - |
Subtraction |
Integer and real type |
Integer and real type |
| * |
Multiplication |
Integer and real type |
Integer and real type |
| / |
Division |
Integer and real type |
Integer and real type |
| MoD |
Remainder |
Integer |
Integer |
| Div |
Division |
Integer |
Integer |
2. logical operators
The operation objects and return values of logical operators are Boolean, as shown in table 1-10.
Table 1-10 logical operators of Object Pascal
| Operator |
Operation |
Operation Data Type |
| Not |
Non |
Boolean |
| And |
And |
Boolean |
| Or |
Or |
Boolean |
| XOR |
Exclusive or |
Boolean |
3. Comparison Operators
The Return Value of the comparison operator is Boolean (true or false). The Relational operators are shown in Table 1-11.
Table 1-11 Object Pascal Relational operators
| Operator |
Operation |
Operation Data Type |
| = |
Equal |
Simple data type, pointer, set, string or string package |
| <> |
Not equal |
Simple data type, pointer, set, string or string package |
| < |
Less |
Simple data type, pointer, set, string or pchar |
| > |
Greater |
Simple data type, pointer, set, string or pchar |
| <= |
Less than or equal |
Simple data type, pointer, set, string or pchar |
| > = |
Greater than or equal |
Simple data type, pointer, set, string or pchar |
| <= |
Subset |
Set |
| > = |
Parent set |
Set |
4. bitwise operators
You can use the bitwise operator to modify the bitwise of a variable. You can perform the bitwise AND, Or, non, exclusive or, or shift to the left (SHL) or right (SHR) of a number.
5. Operator priority
An expression may have multiple operators whose operations are sequential. The priority of different operators in an expression is shown in Table 1-12.
Table 1-12 operator priority
| Operator |
Description |
| () |
Brackets |
| + ,- |
Positive and Negative |
| ^ |
Multiplication party |
| *,/ |
Multiplication and division |
| + ,- |
Addition and subtraction, string connection |
| =,>, <, <=, >=, <> |
Relational operators |
| Not |
Non-logical |
| And |
Logic and |
| Or |
Logic or |
1.7 statement
1. Constant declaration statement
A constant is assigned a value during declaration and cannot be changed during program execution. The following example declares three constants:
Const Pi = 3.14159; bookid = 362; bookname = "Delphi ";
Constants also have types, just like variables. The types of the preceding three constants are real, integer, and string. The constant uses "=" to indicate that the values on both sides are equal.
2. Assignment Statement
The assignment statement is as follows:
Variable: = expression; // variable: = expression;
3. GOTO statement
The GOTO statement can directly jump from one place in the program to another. However, from the perspective of structured program design, do not use goto statements.
The form of the GOTO statement is as follows:
Goto label;
Before using the GOTO statement, you must declare the label. The statement of label declaration is as follows:
Label label1, label2,..., labeln;
4. Compound statements
A group of statements including begin and end are called compound statements. Composite statements can be nested or empty composite statements are allowed. For example:
Begin C: = A; A: = B; B: = C; end;
5. If statement
The structure of the IF statement is as follows:
If <condition> then <Statement>;
You can also add an else clause to the basic statement:
If <condition> then <Statement> else <Statement>;
Note that the <Statement> in the preceding two examples can be a single or compound statement. The IF... then statement is considered a single statement, and there is only one semicolon at the end of the statement (after the then-guided clause in 1st cases, or in else-cited in 2nd Cases
).
For example, in the following code, date is of the integer type, judge if the date value is displayed as "Last Day" between 1 and 10, "middle day" between 11 and 20, and "next day" between 21 and 31 ", if the value is not included in the preceding range, the error date is displayed ":
If (date> = 1) and (date <= 10) Then writeln ('nestial') else if (date> = 11) and (date <= 20) then writeln ('中e') else if (date> = 21) and (date <= 31) Then writeln ('latest') else writeln ('wrong date ');
Note: Before the else clause, the then-guided clause cannot contain semicolons.
6. Case statement
If... then... else statements are suitable for scenarios with fewer options. If there are many options, using the if statement is more troublesome. In this case, the case statement is much easier. The syntax of the case statement is as follows:
Case <expression> of <value >:< statement >;< value >:< statement >;else <statement >;end;
In the following example, S is of the char type. After you enter a character, press the Enter key to confirm the variable S. If it is a lowercase letter or an uppercase letter, "character" is displayed. If it is a number, "Number" is displayed ", the remaining characters are displayed as "Others". The judgment statement is as follows:
Below:
VaR S: Char; begin readln (s); case s of 'A '.. 'Z', 'A '.. 'Z': writeln ('characters'); '0 '.. '9': writeln ('number'); else writeln ('others'); end;
Note: any statement in the case structure can be a single or compound statement. If you need to execute multiple lines of code in some circumstances, you must include them with begin... end.
7. Repeat statement
The repeat statement repeats one or more statements until a certain state is true. The statement starts with repeat and ends with until, followed by the judged Boolean expression. The structure is as follows:
Repeat <Statement> until <condition expression>
For example:
I: = 0; repeat I: = I + 1; writeln (inttostr (I); until I> 5;
When the preceding example is executed, a number ranging from 1 to 6 is displayed. Boolean expression I> 5 is not calculated until the end of the repeat... Until program segment, that is, the repeat statement must be executed at least once.
8. While statement
The difference between the while statement and the repeat statement is that the Boolean expression of the while statement is determined at the beginning of the loop. If the expression result is true, a loop is executed. Otherwise, the loop is exited. The structure is as follows:
While <condition expression> begin <Statement> end;
For example:
I: = 0; while I <5 do begin writeln (inttostr (I); Inc (I); end;
The preceding example shows a number ranging from 0 to 4. The loop is not exited until I is no less than 5.
9. For statement
The program code in the for statement will be executed for a certain number of times. It requires a loop variable to control the number of loops. The type of the cyclic variable can be integer, Boolean, struct, enumeration, or subbounded. The structure is as follows:
For <condition expression> dobegin <Statement> end;
The following example shows a number ranging from 1 to 5:
For I: = 1 to 5 dobegin writeln (inttostr (I); end;
The following example shows the number 5-1:
For I: = 5 downto 1 dobegin writeln (inttostr (I); end;
10. Break statement
By calling break in each loop, the execution process of the program can jump to the end of the loop immediately. For example, if the program jumps out of the loop five times after execution, the Code is as follows:
For I: = 1 to 10 dobegin if I = 5 then break; end;
11. Continue statement
If you want to skip some code in the loop and start the next loop, you can call the continue statement. The following example shows how to continue the next loop when the code after continue is not executed during the execution of 3rd cycles:
For I: = 1 to 5 dobegin if I = 3 then continue; writeln (inttostr (I); end;
12. With statement
When using record-type variables, you can use the with statement to specify some statements for a specific variable, which can simplify the input of code. The with statement is as follows:
With object do statement
For example:
Type temployee = Record Name: String [20]; yearhired: 2000 .. 2010; salary: Double; position: String [20]; end; var EMPLOYEE: temployee; begin with employee do begin name: = 'wang'; yearhired: = 2005; salary: = 10000; position: = 'technic Department '; end;