Arithmetic operators perform basic mathematical operations. Kotlin supports the following arithmetic operators:
+):
val sum = 5 + 3 // sum is 8val difference = 5 - 3 // difference is 2val product = 5 * 3 // product is 15/):
val quotient = 6 / 3 // quotient is 2%):
val remainder = 5 % 3 // remainder is 2Relational operators are used to compare two values. The result of a relational operation is a Boolean value (true or false). Kotlin supports the following relational operators:
==):
val isEqual = 5 == 3 // isEqual is false!=):
val isNotEqual = 5 != 3 // isNotEqual is true>):
val isGreater = 5 > 3 // isGreater is true<):
val isLess = 5 < 3 // isLess is false>=):
val isGreaterOrEqual = 5 >= 3 // isGreaterOrEqual is true<=):
val isLessOrEqual = 5 <= 3 // isLessOrEqual is falseIncrement and decrement operators are used to increase or decrease the value of a variable by one.
++):
Increases the value of a variable by one.
Prefix Increment: ++variable
The variable is incremented first, then its value is used.
Example:
var a = 5
val b = ++a // a is 6, b is 6
Postfix Increment: variable++
The variable's value is used first, then it is incremented.
Example:
var a = 5
val b = a++ // a is 6, b is 5