What are the operators in Euphoria?
Introduction
An operator in Euphoria will perform an action between two or more operands on both sides. Euphoria Programming is a simple and flexible language that also provides a robust pool of operators to help developers achieve a lot. These operators are bundled into four major types, namely:
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
Arithmetic operators
These operators are used to carry out simple algebra and arithmetic computations. They are used to evaluate mathematical operations. They are listed and explained below. For the examples, let x be 10, and y be 30.
-
Addition operator (
+): It adds two operand values,(x+y gives 40). -
Subtraction operator (
-): It subtracts the value of the right operand from the left,(y-x gives 20). -
Division operator (
/): It divides the operand on its left side with that on its right side.(y/x gives 3) -
Multiplication operator (
*): It multiplys both operands, (x*y gives 300). -
Unary minus(-) and unary plus(+): They operate only on a single operand, the one that comes after it from the left to right direction. For example,
(-y gives -30 while +x gives +10). They indicate if a value is positive(+) or negative (-).
Comparison operator
These operators compare the operands at their left and right sides and then decide. Let’s look at the various comparison operators used in Euphoria:
-
Equal (=): It checks for equality between the operands’ left and right sides. If equality exists, it evaluates to true. For example,
(10=30 gives false). -
Not equal (!=): It checks if the operands are not equal in value and if not equal, it returns true. Otherwise, it returns false, (10!=30 gives true).
-
The greater than
>: It compares the operand on its left with the right. It returns true if the left operand isgreaterin value than the right. Otherwise, it returnsfalse. For example,(10>30 returns false). -
The less than (<): It returns true if the value on the right side is less than the value on the left side. For example,
(x<y returns true). -
Less or equal to (
<=): It checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true,(10<=30 returns true). -
Greater or equal to (
>=): It checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomestrue. If not, the condition evaluatesfalse. For example,(10 >= 30 returns false).
Logical Operators
These operators carryout out logic on operands in an expression and make a decision based on the output which is usually boolean.
- Logical AND: It evaluates true if the two operands are none zero and false if otherwise.
- Logical OR:It evaluates
trueif any of the operands have non-zero values and evaluates tofalseonly if both operands are zero values. - Logical XOR: It checks the two operands and will return true only if any of them has a non-zero value. If both operands are zeros(that is false), or ones (true) then it evaluates to false.
- NOT operator: It negates any value. It is actually a unary operator. For example,
NOT Y, will makeYfalse, if it was originallytrue.
Assignment operators
This set of operators assigns whatever is on its right to its left. This is true for the simple assignment operator(=). For example,
name = "jane" this will automatically make name to have the value of jane.
Other not so simple assignment operators are also available. These are not so simple because they are combined with any of the arithmetic operators. This makes them evaluate the expression first before assigning the result to the left operand. These operators include:
- Addition and assignment operator (
+=) - Subtract and assignment operator(
-=) - Division and assignment operator(
/=) - Multiplication and assignment operator (
*=) - The concatenation and assignment operator (
&=)
Note: The
&=operator assigns to the left operand an ANDED value of the operand on the left and the one on the right. For example:X &= Yis the same asX = X & Y.
Concatenation operator
Euphoria also has an operator for concatenation. The & operator is used to concatenate or join two objects. These joined objects will have the sum of the two joining objects as their length.
Example
sequence x, y, z
x = {4, 5, 6}
y = {7}
z = {4, 5, 6} & {7}
print(1,z)
This produces the output: {4,5,6,7}
Code
--declare the variablessequence x, y, z--assign values to the declared variablesx = {4, 5, 6}y = {7}--concatenate the x and y valuesz = {4, 5, 6} & {7}-- print the concatenated valuesprint(1,z)
In the above, we see the concatenation operator & is used to join the two sequences x and y to produce another sequence z. It is the same as the length of the two sequences joined together.