We often perform arithmetic operations in our daily life. We need to calculate the change, measure the area of a room, count the number of people in a queue, and so on. The computer can help us with these operations.
Binary operators
Kotlin has five arithmetic operators:
- addition
+ - subtraction
- - multiplication
* - integer division
/ - modulus
%
These operators are binary, which means they take two values as operands. The operand is the value or variable the operator is being applied to. For example, in expression 1 + 3, 1 and 3 are the operands, and + is the operator.
The piece of code below prints the results of addition, subtraction, and multiplication:
println(13 + 25) // prints 38
println(20 + 70) // prints 90
println(70 - 30) // prints 40
println(30 - 70) // prints -40
println(21 * 3) // prints 63
println(20 * 10) // prints 200
The / operator divides the integer parts of two numbers; the fractional part is discarded. You may read about modulo division in our topic.
println(8 / 3) // prints 2
println(41 / 5) // prints 8
The % operator finds the remainder of a division:
println(10 % 3) // prints 1 because 10 divided by 3 leaves a remainder of 1
println(12 % 4) // prints 0 because 12 divided by 4 leaves no remainder
As you may know, division with negative numbers is different. You may read our topic to find more about the different types of division.
Complex expressions
You can combine arithmetic operations to write complex expressions:
println(1 + 3 * 4 - 2) // prints 11
The calculation order coincides with the rules of arithmetic operations. Multiplication has a higher priority than addition or subtraction, so 3 * 4 is calculated first.
Consider an expression as a part of code that produces a value (for example, 14 + 5 or 15 - 3 * 4). Further, we will take a closer look at expressions.
Use parentheses to specify the order of execution. Let's take a look at the example below:
println((1 + 3) * (4 - 2)) // prints 8
Parentheses can be nested. You can also use them to simplify the notation.
For example, here is a program that prints all digits of the number 54 in reverse order. We will use arithmetic operations to extract the digits.
fun main() {
println(54 % 10) // it prints 4
println((54 / 10) % 10) // it prints 5
}
The program outputs:
4
5Unary operators
A unary operator takes a single value as the operand.
- A unary plus just gives you a value. It is an optional operator, so you omit it in practice:
println(+5) // prints 5
println(+(-5)) // prints -5
- A unary minus negates a value or expression:
println(-8) // prints -8
println(-(100 + 4)) // prints -104
They both have higher precedence than the multiplication and division operators.
Precedence order
Take a look at the precedence order of arithmetic operators, including parentheses. The list is sorted from the highest to the lowest priority.
- Parentheses;
- Unary plus/minus;
- Multiplication, division, and modulus;
- Addition and subtraction.
Mind this order: it's important for correct calculations. Also, you can use the acronym BODMAS (Brackets, Order, Division, Multiplication, Addition, Subtraction) to remember priority.
Conclusion
In this topic, we have covered five main arithmetic operations (+, -, *, /, and %). Of course, you can combine them into complex expressions with parenthesis (to simplify the notation) and with unary operators. However, mind the precedence. The good news is that in Kotlin, the precedence order follows the rules you've learned in high school.