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