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