当前位置:网站首页>[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
2022-07-01 17:39:00 【Janeb1018】
4.Object Pascal Operators and expressions
Operation is the processing and processing of data , Operators are symbols that operate on values of various data types in code .
stay Object Pascal in , Operators are divided into : Monocular operator and binocular operator .
An expression is an operation formula that represents an evaluation rule , Constants are separated by operators and parentheses 、 Variable 、 function 、 Object and other operands .
4.1 Arithmetic operators and arithmetic expressions
1. Arithmetic operator
Object Pascal The arithmetic operator in :
| Operator | name | Example | Operator | name | Example |
|---|---|---|---|---|---|
| + | Correct | +a | Div | Divide the whole number by | a Div b |
| - | Take the negative | -b | Mod | Seeking remainder | a Mod b |
| * | ride | a*b | + | Add | a+b |
| / | except | a/b | - | reduce | a-b |
The operation result is automatically converted to high precision ;
Division operations / Whether integer or real , The results are all real ;
Div and Mod The operand of must be an integer , The result is an integer ;
2. The precedence of arithmetic operators
| Operator | priority |
|---|---|
| + Correct 、- Take the negative | 1 |
| *、/ | 2 |
| Div、Mod | 3 |
| +、- | 4 |
The same level operation is performed from left to right , Right parenthesis in expression , Calculate the expression in parentheses first , If there are multiple brackets , First calculate the expression in the inner bracket .
3. Arithmetic expressions
accord with Object Pascal The specified arithmetic expression should consider three aspects : grammar 、 priority 、 type . Let's give an example of :
for example :
| The mathematical formula | Arithmetic expressions |
|---|---|
| x/(yz) | x/(y*z) |
| (2x-y)/x | (2.0*x-y)/x |
| lg4.5+0.3ln4.5 | log10(4.5)+0.3*ln(4.5) |
| (5x^2.3+3)/(3/5) | (5*Power(x, 2.3)+3.0)/(3.0/5.0) |
4.2 Logical operators and Boolean expressions
4.2.1 Logical operators
Logical operators include : Boolean operator 、 Bitwise operators and relational operators .
1. Boolean operator
Boolean operators can only operate on Boolean operands , The result is Boolean . Basic Boolean operators :
NOT “ Not ” Operator
Calculate the opposite number of operands , Monocular operator .
x NOT x True False False True AND “ And ” Operator
Only if both operands are True when , The result of the operation is True.
x y x AND y False False False False True False True True True True Flase False OR “ or ” Operator
As long as one of the two operands is True, The result of the calculation is True.
x y x OR y False False False False True True True True True True False True XOR “ Exclusive or ” Operator
When two operands are different, the result is True
x y x XOR y Flase False False False True True True True False True False True
2. An operator
Bit operation is to perform operations on the binary bits of integer operands ,“ position ” Refers to binary bits , Bitwise operation means that adjacent operation results are not considered , Only the corresponding bits in the two operands are calculated .
An operator
| The operator | give an example | Functional specifications |
|---|---|---|
| NOT | NOT x | take x Each binary bit of is negated , namely 1 Turn into 0,0 Turn into 1. |
| AND | x AND y | And the corresponding bits of the two operands , A fellow 1 The result is 1 |
| OR | x OR y | Or the corresponding bits of two operands , A fellow 0 The result is 0 |
| XOR | x XOR y | XOR the corresponding bits of two operands , When the corresponding bit is different, it is 1 |
| SHL | x SHL y | take x The binary bit of moves to the left y position , Move left 1 A bit is a ride 2 |
| SHR | x SHR y | take x The binary bit of moves to the right y position , Move right 1 Bit is equivalent to dividing 2 |
Example :
| x | y | operation | result |
|---|---|---|---|
| 00100011 | NOT x | 11011100 | |
| 00100011 | 11101110 | x AND y | 00100010 |
| 00100011 | 11101110 | x OR y | 11101111 |
| 00100011 | 11101110 | x XOR y | 11001100 |
| 124=11111100 | 2 | x SHL y | 11111000 |
| 124=11111100 | 2 | x SHR y | 01111110 |
3. Relational operator
Relational operators are used to compare the values of two data of the same type , The result is Boolean .
Relational operator
| Relational operator | operation | Operand type |
|---|---|---|
| = | be equal to | Simple type 、 character string 、 Variable type 、 class 、 Class reference 、 The pointer 、 Collection types |
| <> | It's not equal to | Simple type 、 character string 、 Variable type 、 class 、 Class reference 、 The pointer 、 Collection types |
| < | Less than | Simple type 、 character string 、 Variable type |
| > | Greater than | Simple type 、 character string 、 Variable type |
| <= | Less than or equal to | Simple type 、 character string 、 Variable type 、 Collection types |
| >= | Greater than or equal to | Simple type 、 character string 、 Variable type 、 Collection types |
Relational operation of set type : hypothesis A and B It's two sets , be :
if A and B The elements of are exactly the same , be A=B by True, otherwise A<>B by True;
if A Any element of is also B The elements of , be A<=B by True, said A yes B Subset ;
if B Any element of is also A The elements of , be A>=B by True, said B yes A Subset ;
4.2.2 Boolean expression
Boolean expressions consist of Boolean operators and operands of boolean type . Boolean expressions can be used to describe complex judgment conditions .
for example : To describe whether a real number is in an interval [10,100] Inside , Then Boolean expression :
(x >= 10.0) and (x <= 100.0)
To describe whether a real number is in an interval [10,100] outside , Then Boolean expression :
(x < 10.0) or (x > 100.0)
4.3 String operators
Object Pascal String operator for :+, Used to connect two or more strings .
Format of string expression :
< String expressions > + {< character string >|< character >}Example :
'Hello world' + '!' // The result is 'Hello world!' 'Delphi' + ' Programming ' // The result is 'Delphi Programming ' '12 3' + '4' + '5' + 'abc' + 'xyz' // The result is '12 345abcxyz'
4.4 Operator precedence
In addition to the above operators , And pointer operators 、 Set operators 、 Class operators and address taking operators ( We will learn these gradually in the following content ), All operators have priority ,Object Pascal The priority of the operator in the :
| priority | Operator | describe |
|---|---|---|
| 1 | @、NOT、+、- | Monocular operator |
| 2 | *、/、DIV、MOD、AND、SHL、SHR | Multiplication, division and some logical operators |
| 3 | +、-、OR、XOR | Add, subtract, and or operators |
| 4 | =、<>、<、>、<=、>=、in、is | Relationship 、 Set members and type comparison operators |
Some of the above operators are not learned so far , In the following chapters, we will gradually learn .
边栏推荐
- How to write good code - Defensive Programming Guide
- (12) About time-consuming printing
- Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
- 中国PBAT树脂市场预测及战略研究报告(2022版)
- libcurl下载文件的代码示例
- Kernel stray cat stray dog pet adoption platform H5 source code
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
- Hidden Markov model (HMM): model parameter estimation
- 多线程并发之CountDownLatch阻塞等待
猜你喜欢

智能运维实战:银行业务流程及单笔交易追踪

Kernel stray cat stray dog pet adoption platform H5 source code

Pytest learning notes (13) -allure of allure Description () and @allure title()

SQL注入漏洞(Mysql与MSSQL特性)

DNS

换掉UUID,NanoID更快更安全!

vulnhub靶场-hacksudo - Thor

In aks, use secret in CSI driver mount key vault

Why should you consider using prism

Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
随机推荐
ACL 2022 | decomposed meta learning small sample named entity recognition
Radhat builds intranet Yum source server
Shenyu gateway development: enable and run locally
Sword finger offer II 105 Maximum area of the island
中国PBAT树脂市场预测及战略研究报告(2022版)
多线程并发之CountDownLatch阻塞等待
Depth first traversal and breadth first traversal [easy to understand]
Rotation order and universal lock of unity panel
两数之和c语言实现[通俗易懂]
Kia recalls some K3 new energy with potential safety hazards
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
ACL 2022 | 分解的元学习小样本命名实体识别
The difference and relationship between iteratible objects, iterators and generators
Can hero sports go public against the wind?
(17) DAC conversion experiment
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
Petrv2: a unified framework for 3D perception of multi camera images
Vulnhub range hacksudo Thor
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion