📜  Euphoria-运算符

📅  最后修改于: 2020-11-04 07:43:31             🧑  作者: Mango


 

Euphoria提供了一组丰富的运算符来操纵变量。我们可以将所有的幸福感运算符分为以下几类:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 赋值运算符
  • 杂项运算符

算术运算符

算术运算运算符在数学表达式中的使用方式与在代数中使用的方式相同。下表列出了算术运算运算符。假设整数变量A保持10并且变量B保持20然后-

显示范例

Operator Description Example
+ Addition – Adds values on either side of the operator A + B will give 30
Subtraction – Subtracts right hand operand from left hand operand A – B will give -10
* Multiplication – Multiplies values on either side of the operator A * B will give 200
/ Division – Divides left hand operand by right hand operand B / A will give 2
+ Unary plus – This has no impact on the variable value. +B gives 20
Unary minus – This creates a negative value of the given variable. -B gives -20

关系运算符

Euphoria语言支持以下关系运算符。假设变量A持有10,变量B持有20,则-

显示范例

Operator Description Example
= Checks if the value of two operands are equal or not, if yes then condition becomes true. (A = B) is not true.
!= Checks if the value 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.

逻辑运算符

下表列出了逻辑运算符。假设布尔变量A保持1,变量B保持0,然后-

显示范例

Operator Description Example
and Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A and B) is false.
or Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (A or B) is true.
xor Called Logical XOR Operator. Condition is true if one of them is true, if both operands are true or false then condition becomes false. (A xor B) is true.
not Called Logical NOT Operator which negates the result. Using this operator, true becomes false and false becomes true not(B) is true.

您还可以将这些运算符应用于1或0以外的数字。约定为:0表示false,非零表示true

赋值运算符

Euphoria语言支持以下赋值运算符-

显示范例

Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C – A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
&= Concatenation operator C &= {2} is same as C = {C} & {2}

–赋值语句中使用的等号’=’不是运算符,它只是语法的一部分。

杂项运算符

Euphoria语言支持的其他运算符很少。

运营商

可以使用“&”运算符将任意两个对象串联起来。结果是一个序列,其长度等于串联对象的长度之和。

例如-

#!/home/euphoria-4.0b2/bin/eui

sequence a, b, c
a = {1, 2, 3}
b = {4}
c = {1, 2, 3} & {4}

printf(1, "Value of c[1] %d\n", c[1] )
printf(1, "Value of c[2] %d\n", c[2] )
printf(1, "Value of c[3] %d\n", c[3] )
printf(1, "Value of c[4] %d\n", c[4] )

这产生以下结果-

Value of c[1] 1
Value of c[2] 2
Value of c[3] 3
Value of c[4] 4

Euphoria运算符的优先级

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

例如,x = 7 + 3 * 2

此处,x被分配为13,而不是20,因为运算符*的优先级高于+。

因此,它首先从3 * 2开始,然后加到7。

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

Category Operator Associativity
Postfix function/type calls
Unary + – ! not Right to left
Multiplicative * / Left to right
Additive + – Left to right
Concatenation & Left to right
Relational > >= < <= Left to right
Equality = != Left to right
Logical AND and Left to right
Logical OR or Left to right
Logical XOR xor Left to right
Comma , Left to right