Operators
Operators are special characters or keywords that represent a mathematical or logical action to be performed in a computer program. For example, the mathematical operator + is used to add two numbers, while the logical operator and is used to verify if two expressions are true. Operators are used in combination with values or variables to create expressions that can be evaluated to a result.
When an expression has several operators, the order in which they are calculated is in Operator precedence.
Arithmetic operators
Arithmetic operators allow performing basic mathematical operations such as addition, subtraction, multiplication, and division. The arithmetic operators in JARU are:
| Operator | Description | Example |
|---|---|---|
+ | Add two numbers | 5 + 2 = 7 |
- | Subtract two numbers | 5 - 2 = 3 |
* | Multiply two numbers | 5 * 2 = 10 |
/ | Divide two numbers; the result is always a float, even between integers | 5 / 2 = 2.5 |
div | Integer division: keeps the integer part, truncating toward zero | 5 div 2 = 2, -5 div 2 = -2 |
% | Get the remainder of a division | 5 % 2 = 1 |
mod | The same as % | 5 mod 2 = 1 |
++ | Increment a number by one | x++ |
-- | Decrement a number by one | x-- |
It's possible to use these operators to create more complex mathematical expressions, and they can be used with both variables and constant values.
var num1 = 5
var num2 = 2
// Addition
println(num1 + num2) // 7
// Subtraction
println(num1 - num2) // 3
// Multiplication
println(num1 * num2) // 10
// Division
println(num1 / num2) // 2.5
// Integer division
println(num1 div num2) // 2
// Modulo
println(num1 % num2) // 1
// Increment
num1++
println(num1) // 6
// Decrement
num2--
println(num2) // 1
- The remainder takes the sign of the first number (
-7 % 3gives-1), and it also works with decimals:7.5 % 2gives1.5. - Dividing by zero with
/ordivstops the program with the error “Division by zero”; with%ormod, with “Modulo by zero.”. You can catch it withtry: see Errors and exceptions. +also joins two strings,"Hello, " + name, but not a string and a number: see Joining strings.x++gives the valuexhad before adding one, and++xthe value after: ifais 5,a++ + 10gives 15 and++a + 10gives 16; either wayaends up as 6.--works the same way.- They work with a variable, a property or an element of a list or a map:
lives++,player.lives--,list[0]++,++points["ana"]. With anything else, like5++, the compiler gives an error.
Assignment operators
Assignment operators are those used to assign a value to a variable. In JARU, the assignment operators are:
| Operator | Description |
|---|---|
= | Assigns the value on the right of the expression to the variable on the left |
+= | Adds the value on the right of the expression to the current value of the variable on the left |
-= | Subtracts the value on the right of the expression from the current value of the variable on the left |
*= | Multiplies the current value of the variable on the left by the value on the right of the expression |
/= | Divides the current value of the variable on the left by the value on the right of the expression |
%= | Calculates the remainder of dividing the current value of the variable on the left by the value on the right of the expression and assigns it to the variable on the left |
var num1 = 10
num1 += 2 // num1 = 12
num1 -= 5 // num1 = 7
num1 *= 3 // num1 = 21
num1 /= 2 // num1 = 10.5
num1 %= 3 // num1 = 1.5
The bitwise operations have no short form: write flags = flags | 4.
Comparison operators
Comparison operators in JARU are similar to those in other programming languages. These operators compare two values and return a boolean value (true or false).
| Operator | Description |
|---|---|
== | Checks if two values are equal |
!= | Checks if two values are different |
> | Checks if the value on the left is greater than the value on the right |
< | Checks if the value on the left is less than the value on the right |
>= | Checks if the value on the left is greater than or equal to the value on the right |
<= | Checks if the value on the left is less than or equal to the value on the right |
var num1 = 5
var num2 = 10
// using the == operator
if (num1 == num2) then
println("num1 is equal to num2")
else
println("num1 is not equal to num2")
end
// using the != operator
if (num1 != num2) then
println("num1 is different from num2")
else
println("num1 is equal to num2")
end
// using the > operator
if (num1 > num2) then
println("num1 is greater than num2")
else
println("num1 is not greater than num2")
end
// using the < operator
if (num1 < num2) then
println("num1 is less than num2")
else
println("num1 is not less than num2")
end
==and!=compare numbers by their value (1 == 1.0givestrue) and strings by their content.- Two lists, maps or objects are only equal if they are the same one:
[1, 2] == [1, 2]givesfalse. <,>,<=and>=compare numbers. With strings they don't look at alphabetical order:"a" < "b"givesfalse(see Comparing strings).
Logical operators
Logical operators are used to combine, compare, and evaluate logical expressions and are used to make decisions based on the value of boolean expressions (which can only have two possible values: true or false).
| Operator | Description |
|---|---|
and | Returns true if both expressions are true |
or | Returns true if at least one of the expressions is true |
not | Inverts the logical value of the expression |
! | The same as not |
^ | Evaluates to true if one of the expressions is true, but not both |
var x = (5 > 3) and (2 < 4) // x is true
var y = (5 > 3) or (2 > 4) // y is true
var z = not(5 > 3) // z is false
var v = !(5 > 3) // v is false
var w = (5 > 3) ^ (2 > 4) // w is true
var x = 5
var y = 8
var z = 10
println(x < y and y < z) // true
println(x > y or y < z) // true
println(not(x > y)) // true
In the first case, the comparison x < y is true and y < z is also true, so the expression x < y and y < z is true.
In the second case, the comparison x > y is false but y < z is true, so the expression x > y or y < z is true.
In the third case, the comparison x > y is false, so not(x > y) is true.
and and or stop calculating as soon as they know the result. In i < list.size() and list[i] > 0, if i is outside the list, list[i] is never read. And they always give true or false: 0 or 5 gives true, not 5.
and and or have the same precedenceThey are calculated from left to right: true or false and false gives false, because it's computed as (true or false) and false. In other languages, and goes before or. Use parentheses to make the order you want clear: true or (false and false) gives true.
not and ! are calculated before comparisons: not 1 == 2 is (not 1) == 2, and gives false. To negate a comparison, put it in parentheses, as in not(x > y). The ^ operator is also calculated before comparisons: write each comparison in parentheses, as in (5 > 3) ^ (2 > 4). The order of all the operators is in Operator precedence.
Bitwise operators
They work with the bits of integer numbers: they are used to read a sensor's registers, for pin masks or to split a 0xRRGGBB color into its parts.
| Operator | What it does | Example |
|---|---|---|
& | AND: a 1 where both have a 1 | 0x0F & 0x3C gives 12 (0x0C) |
| | OR: a 1 where either has a 1 | 0x0F | 0x30 gives 63 (0x3F) |
^ | Exclusive OR: a 1 where only one of the two has a 1 | 0x0F ^ 0x3C gives 51 (0x33) |
~ | Inverts all the bits | ~5 gives -6 |
<< | Shifts the bits to the left | 1 << 4 gives 16 |
>> | Shifts the bits to the right | 256 >> 4 gives 16 |
var color = 0xFF8800 // orange, in 0xRRGGBB
var red = (color >> 16) & 0xFF
var green = (color >> 8) & 0xFF
var blue = color & 0xFF
println(red, " ", green, " ", blue) // 255 136 0
- They are for integers. With decimals,
&,|and^drop the decimal part first (5.7 & 3gives1),<<and>>stop the program (“Both operands for the '<<' operator must be integers.”), and~gives-1without a warning. - Integers are 32-bit:
1 << 31gives-2147483648. Shift by 0 to 31 positions. >>fills with zeros from the left, also for negative numbers:-8 >> 1gives2147483644, not-4. To divide a negative number by 2, usediv.- With two booleans,
&,|and^give a boolean, but they always calculate both sides: they don't stop early likeandandor. ^is not the power operator:2 ^ 3gives1. For powers there isMath.pow(2, 3), in the Math module.- They are calculated before comparisons, and
&,|and^have the same precedence among themselves:4 | 1 & 2gives0, because it is(4 | 1) & 2. Use parentheses.
All the known issues of JARU, together, are in Known issues.
Conditional (ternary) operator
The conditional operator ?, also known as the ternary operator, is a conditional operator used in many programming languages, including JARU. The ternary operator allows making a decision based on a boolean expression and executing different actions depending on the result of the evaluation.
The general syntax of the ternary operator is as follows:
condition ? true_expression : false_expression
The condition is a boolean expression that evaluates to true or false. If the condition is true, true_expression is evaluated; otherwise, false_expression is evaluated.
var age = 18
var message = (age >= 18) ? "You are an adult" : "You are a minor"
println(message) // You are an adult
In this example, the condition is age >= 18, which evaluates to true because age equals 18. Therefore, the true_expression is "You are an adult", which is assigned to the message variable.
The ternary operator can make code shorter, but also harder to understand if it's overused. Use it to choose between two simple values.
Nested ternary operators
It's possible to nest ternary operators to make complex decisions based on multiple conditions:
var age = 18
var message = (age >= 18) ? "You are an adult" : (age >= 13) ? "You are a teenager" : "You are a child"
println(message)
In this example, two nested conditions are evaluated to determine the message that should be displayed based on a person's age. If the age is greater than or equal to 18, the message will be "You are an adult". If not, the next condition is evaluated: if the age is greater than or equal to 13, the message will be "You are a teenager". If the age doesn't meet any of these conditions, the message will be "You are a child".