Operators¶
Operators are used to perform operations on variables and constants. This section talks about the most frequently used operators in XS.
1. Arithmetic operators¶
Operator | Expression | Description | Example | Result |
---|---|---|---|---|
+ |
a + b |
Adds a and b . |
2 + 3 |
5 |
- |
a - b |
Subtracts b from a . |
5 - 3 |
2 |
* |
a * b |
Multiplies a by b . |
2 * 3 |
6 |
/ |
a / b |
Divides a by b . |
6 / 2 |
3 |
% |
a % b |
Calculates the remainder of a divided by b . |
5 % 2 |
1 |
2. Comparison operators¶
Operator | Expression | Description | Example | Result |
---|---|---|---|---|
== |
a == b |
Checks if a is equal to b . |
2 == 3 |
false |
!= |
a != b |
Checks if a is not equal to b . |
2 != 3 |
true |
< |
a < b |
Checks if a is less than b . |
2 < 3 |
true |
<= |
a <= b |
Checks if a is less than or equal to b . |
2 <= 3 |
true |
> |
a > b |
Checks if a is greater than b . |
2 > 3 |
false |
>= |
a >= b |
Checks if a is greater than or equal to b . |
2 >= 3 |
false |
3. Logical operators¶
Operator | Expression | Description | Example | Result |
---|---|---|---|---|
&& |
a && b |
Checks if a and b are both true. |
true && false |
false |
|| | a || b |
Checks if a or b is true. |
true || false |
true |
4. Assignment operators¶
Assignment operators involve assigning a value to a variable. The variable to which the value is assigned is always on the left side of the operator, and must have been defined before.
Operator | Expression | Description | Example | Result |
---|---|---|---|---|
= |
a = b |
Assigns the value of b to a . |
a = 2 |
The value of a is now 2 |
+= |
a += b |
Adds b to a , then assigns the result to a . |
a += 2 |
The value of a has increased by 2 |
-= |
a -= b |
Subtracts b from a , then assigns the result to a . |
a -= 2 |
The value of a has decreased by 2 |
*= |
a *= b |
Multiplies a by b , then assigns the result to a . |
a *= 2 |
The value of a has doubled |
/= |
a /= b |
Divides a by b , then assigns the result to a . |
a /= 2 |
The value of a has halved |
%= |
a %= b |
Calculates the remainder of a divided by b , then assigns the result to a . |
a %= 2 |
The value of a has changed |
5. Increment and decrement operators¶
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
Operator | Expression | Description | Example | Result |
---|---|---|---|---|
++ |
a++ |
Increases the value of a by 1, then assigns the result to a . |
a++ |
The value of a has increased by 1 |
-- |
a-- |
Decreases the value of a by 1, then assigns the result to a . |
a-- |
The value of a has decreased by 1 |
6. Concatenation operator¶
The concatenation operator is used to combine two strings into one. It is
represented by the +
symbol. For example, "Hello" + " " + "world"
will result in the string "Hello world"
.
It is also possible to concatenate a string with any other data type. For
that, the quotation marks are not used. For example, "Hello " + 2
will
result in the string "Hello 2"
.
7. Ternary operator¶
The ternary operator is a special operator that takes three operands. It is
used to shorten an if
statement.
Syntax of the ternary operator
The ternary operator evaluates condition
. If it is true, it evaluates
expression1
and returns its value. Otherwise, it evaluates expression2
and returns its value.
Some examples of the ternary operator
8. Operator precedence¶
Operator precedence determines the order in which operators are evaluated. For
example, in the expression 2 + 3 * 4
, the multiplication is evaluated
first, then the addition. This is because the multiplication operator has a
higher precedence than the addition operator.
Operators with a higher precedence are evaluated before operators with a lower precedence.
Precedence | Operator(s) |
---|---|
1 | ++ , -- |
2 | * , / , % |
3 | + , - |
4 | < , <= , > , >= |
5 | == , != |
6 | && |
7 | || |
8 | = , += , -= , *= , /= , %= |
9 | ? : |
9. Using parentheses¶
Parentheses can be used to override the default precedence of operators. For
example, in the expression 2 + 3 * 4
, the multiplication is evaluated
first, then the addition. However, if you want the addition to be evaluated
first, you can use parentheses: (2 + 3) * 4
.
Parentheses can be nested, and the expression inside the innermost pair of parentheses is evaluated first.