Instruction 1

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Operators and Operations

Operators are symbols that enable you to perform specific actions on data. Data types need specific operations for their particular type. You can’t multiply true and false, for instance. Data operations are time-consuming for people to calculate. But computers are excellent at performing them. As you continue to learn, you’ll see the speed of these calculations. Kotlin supports many different types of operators and operations by default. The data involved in an operation is an operand.

Using Arithmetic Operators

Arithmetic or mathematical operators perform basic mathematical operations on data. These operators operate on numerical types. The same operators may be used with other data types. For instance, adding strings: “Kod” + “eco” = “Kodeco”. For strings, the implementation is different, but the result has a similar meaning. Basic mathematical operations supported in Kotlin are:

Addition

This operator performs a sum of the operands. + is the addition operator:

fun main() {
  println(12 + 4) // Two raw values as operands

  val left = 12
  println(left + 4) // A variable and a raw value as operands

  val right = 4
  println(left + right) // Two variables as operands
}
16
16
16

Subtraction

This performs a deduction of the value on the right-hand side of the operand from the value on the left-hand side of the operand. Like the addition operator above, it works with both raw values and variables. You use the - symbol for this operator:

fun main() {
  val left = 12
  println(left - 4) // A variable and a raw value as operands
}
8

Multiplication

The multiplication operator multiplies the operands. This operator is implemented by the * symbol:

fun main() {
  val left = 12
  println(left * 4) // A variable and a raw value as operands
}
48

Division

The division operator divides the left operand by the right operand. The forward slash / is the symbol for the division operator. Just as division by 0 is mathematically incorrect, dividing by 0 will result in an error for integers and Infinity for floats and doubles.

fun main() {
  val left = 12
  println(left / 4) // A variable and a raw value as operands
}
3

Modulus

This operator returns the remainder after performing a division between the operands. It’s represented by the symbol %:

fun main() {
  val left = 12
  println(left % 4) // A variable and a raw value as operands
}
0

Increment

The increment operator increases a numerical value by 1 and updates the variable containing the data. It’s in the form of ++. It doesn’t work on raw values, variables, or objects only. It doesn’t work for all numerical types. The variable must be mutable since it’ll be updated at the end of the operation:

fun main() {
  var count = 12
  println(count++)
}
Fig. 3.1 - 'Variable in never used' warning
Sor. 9.5 - 'Xiniihti uw sudih emox' gendanw

fun main() {
  var count = 12
  println(count++) // Prints 12
  println(count) // Prints 13
}
12
13
fun main() {
  var count = 12
  println(++count) // Prints 13
}

Decrement

The decrement operator reduces a numerical value by 1 and updates the operand. It works on variables or objects only. It doesn’t work with every kind of numerical data, and the variable must be mutable. Like the increment operator, the decrement operator may proceed with count-- or precede --count, the data’s variable. The following example shows the pre-decrement and post-decrement operators:

fun main() {
  var count = 12
  println(count--) // Prints 12
  println(count) // Prints 11
  println(--count) // Prints 10
}
12
11
10
See forum comments
Download course materials from Github
Previous: Introduction Next: Instruction 2