📜  Pascal-运营商

📅  最后修改于: 2020-11-03 16:13:44             🧑  作者: Mango


运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 Pascal允许以下类型的运算符-

  • 算术运算运算符
  • 关系运算符
  • 布尔运算符
  • 位运算符
  • 集合运算符
  • 字符串运算符

让我们一一讨论算术,关系,布尔和位运算符。稍后我们将讨论集合运算符和字符串运算。

算术运算符

下表显示了Pascal支持的所有算术运算运算符。假设变量A持有10,变量B持有20,则-

显示范例

Operator Description Example
+ Adds two operands A + B will give 30
Subtracts second operand from the first A – B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by denominator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0

关系运算符

下表显示了Pascal支持的所有关系运算符。假设变量A持有10,变量B持有20,则-

显示范例

Operator Description Example
= Checks if the values of two operands are equal or not, if yes, then condition becomes true. (A = B) is not true.
<> Checks if the values of two operands are equal or not, if values are not equal, then condition becomes true. (A <> B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes, then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes, then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes, then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes, then condition becomes true. (A <= B) is true.

布尔运算符

下表显示了Pascal语言支持的所有布尔运算符。所有这些运算符都对布尔操作数起作用,并产生布尔结果。假设变量A为真,变量B为假,则-

显示范例

Operator Description Example
and Called Boolean AND operator. If both the operands are true, then condition becomes true. (A and B) is false.
and then It is similar to the AND operator, however, it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. (A and then B) is false.
or Called Boolean OR Operator. If any of the two operands is true, then condition becomes true. (A or B) is true.
or else It is similar to Boolean OR, however, it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. (A or else B) is true.
not Called Boolean NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. not (A and B) is true.

位运算符

按位运算符对位进行运算并执行逐位操作。所有这些运算符都对整数操作数起作用,并产生整数结果。按位和(&),按位或(|)和按位非(〜)的真值表如下-

p q p & q p | q ~p ~q
0 0 0 0 1 1
0 1 0 1 1 0
1 1 1 1 0 0
1 0 0 1 0 1

假设A = 60;和B = 13;现在以二进制格式,它们将如下所示-

A = 0011 1100

B = 0000 1101

—————–

A&B = 0000 1100

A ^ B = 0011 0001

〜A = 1100 0011

下表列出了Pascal支持的按位运算符。假设变量A保持60,变量B保持13,则:

显示范例

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12, which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61, which is 0011 1101
! Binary OR Operator copies a bit if it exists in either operand. Its same as | operator. (A ! B) will give 61, which is 0011 1101
~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. (~A ) will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240, which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

请注意,Pascal的不同实现在按位运算运算符有所不同。 Free Pascal,我们在这里使用的编译器,但是支持以下按位运算运算符-

Operators Operations
not Bitwise NOT
and Bitwise AND
or Bitwise OR
xor Bitwise exclusive OR
shl Bitwise shift left
shr Bitwise shift right
<< Bitwise shift left
>> Bitwise shift right

Pascal中的运算符优先级

运算符优先级确定表达式中术语的分组。这会影响表达式的求值方式。某些运算符具有更高的优先级;例如,乘法运算符的优先级高于加法运算符。

例如x = 7 + 3 * 2;在这里,x被赋值为13,而不是20,因为运算符*的优先级比+高,因此它首先与3 * 2相乘,然后加到7。

在此,优先级最高的运算符出现在表格的顶部,优先级最低的运算符出现在表格的底部。在表达式中,优先级较高的运算符将首先被评估。

显示范例

Operator Precedence
~, not, Highest
*, /, div, mod, and, &
|, !, +, -, or,
=, <>, <, <=, >, >=, in
or else, and then Lowest