Skip to main content

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.

Arithmetic Operators

Arithmetic operators allow performing basic mathematical operations such as addition, subtraction, multiplication, and division. The arithmetic operators in JARU are:

OperatorDescriptionExample
+Add two numbers5 + 2 = 7
-Subtract two numbers5 - 2 = 3
*Multiply two numbers5 * 2 = 10
/Divide two numbers5 / 2 = 2.5
%Get the remainder of a division5 % 2 = 1
++Increment a number by onex++
--Decrement a number by onex--

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

// Modulo
println(num1 % num2) // 1

// Increment
num1++
println(num1) // 6

// Decrement
num2--
println(num2) // 1

Assignment Operators

Assignment operators are those used to assign a value to a variable. In JARU, the assignment operators are:

OperatorDescription
=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
num1 = 10
num1 += 2 // num1 = 12
num1 -= 5 // num1 = 7
num1 *= 3 // num1 = 21
num1 /= 2 // num1 = 10.5
num1 %= 3 // num1 = 1.5

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).

OperatorDescription
==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

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).

OperatorDescription
andReturns true if both expressions are true
orReturns true if at least one of the expressions is true
notInverts the logical value of the expression
xorEvaluates to true if one of the expressions is true, but not both
x = (5 > 3) and (2 < 4)  // x is true 
y = (5 > 3) or (2 > 4) // y is true
z = not(5 > 3) // z is false
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.

Priority

It's important to remember that the and operator has higher priority than or, so when both are in an expression, and will be evaluated first.

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) // Output: "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.

Moderate Use

It's important to note that the ternary operator can help make code more readable and concise, but it can also make code harder to understand if it's overused or used inappropriately. Therefore, it's important to use it in moderation and only when appropriate.

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, three 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".