Euphoria运算符 - Euphoria教程

Euphoria 提供了一套丰富的运算符操纵变量。我们可以把所有的运算符分成以下几组:

  • 算术运算符

  • 关系运算符

  • 逻辑运算符

  • 赋值运算符

  • 其他运算符

算术运算符:

算术运算符用于在数学表达式中,以同样的方式,它们被用在代数。下表列出了算术运算符:

假设整数变量A=10和变量B=20:

算术运算符示例

运算符 描述 示例
+ 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:

关系运算符例子

运算符 描述 示例
= 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,则:

逻辑运算例子

运算符 描述 示例
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 数字。其规则是:零表示false 和非零的意味着true。

赋值运算符:

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

赋值运算例子

运算符 描述 例子
= 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。

这里具有最高优先级的运算符出现在上面的表中,那些与最低的出现在底部。在一个表达式,将先评估较高优先级运算符。

分类 运算符 关联性
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