Kotlin Operators With Examples

 In this post, we will know about Kotlin operators. Kotlin operators are similar to other programming language operators. 

Basically, operators are symbols that tell the compiler. What operations to be formed on the operands.

How to perform the operation on a variable

If you are a programmer. Then you already know about variables. And different data types. Do you know how to perform an operation on a variable? If you don't know. Then don't worry I will tell you how to perform an operation on the variable.

Have you heard about Kotlin operators? No, Kotlin operators are used to performing operations on variables. These variables are also known as operands.

Kotlin Operators

Kotlin Operators having sets of operators to perform operations in variables.

1. Arithmetic operator (+ - * / %)

2. Comparison operators(== != < > <= >=)

3. Assignment operator(= += -= *= /= %=)

4. Increment and decrement operator(-- ++)

1. Arithmetic operator 

Arithmetic operators in Kotlin are used to performed arithmetic operations. Addition, Subtraction, Multiplication, Divide, and Modulus are arithmetic operators.

Example

Addition

fun main() {

    var num1 = 5

    var num2 = 6

    print("Sum is ${num1 + num2}")

}

Output

Kotlin Operators

Subtraction

fun main() {

    var num1 = 6

    var num2 = 5

    print("Difference is ${num1 - num2}")

}

Output

Difference is 1

Multiplication

fun main() {

    var num1 = 6

    var num2 = 5

    print("Multiplication is ${num1 * num2}")

}

Output

Multiplication is 30

Division

fun main() {

    var num1 = 6

    var num2 = 2

    print("Division is ${num1 / num2}")

}

Output

Division is 3

Modulus

fun main() {

    var num1 = 6

    var num2 = 5

    print("Remainder is ${num1 % num2}")

}

Output

Remainder is 1

2. Comparison operators

In Kotlin comparison operators are used to comparing the two values. It checks whether two values are equal or not. One value is greater than another value or not. 

Comparison Kotlin operators are helpful to take decisions in our programs. Sometimes we have to choose only one statement from all. In such case comparison, operators are very helpful.

Comparison Kotlin operators return boolean values. It returns true. If conditions are correct. It returns false. If conditions are incorrect.

Example of comparison operators

Equal To

fun main() {

    var num1 = 6

    var num2 = 5

    print("Result ${num1 == num2}")

}

Output

Result false

Not Equal To

fun main() {

    var num1 = 6

    var num2 = 5

    print("Result ${num1 != num2}")

}

Output

Result true

Less Than

fun main() {

    var num1 = 6

    var num2 = 5

    print("Result ${num1 < num2}")

}

Output

Result false

Greater Than

fun main() {

    var num1 = 6

    var num2 = 5

    print("Result ${num1 > num2}")

}

Output

Result true

Less Than or  Equal To

fun main() {

    var num1 = 6

    var num2 = 6

    print("Result ${num1 <= num2}")

}

Output

Resul true

Greater Than or Equal To

fun main() {

    var num1 = 3

    var num2 = 6

    print("Result ${num1 >= num2}")

}

Output

Result false

3. Assignment operator

In Kotlin assignment operators are used to assign the value to the operands. There are different assignment operators are used in Kotlin. All having their own importance.

Example

Assign

fun main() {

    var num = 3

    print(num)

}

Output

3

Add Assign

fun main() {

    var num1 = 3

    var num2 = 2

    num1 += num2

    print(num1)

}

Output

5

Subtract Assign

fun main() {

    var num1 = 3

    var num2 = 2

    num1 -= num2

    print(num1)

}

Output

1

Multiply Assign

fun main() {

    var num1 = 3

    var num2 = 2

    num1 *= num2

    print(num1)

}

Output

6

Divide Assign

fun main() {

    var num1 = 6

    var num2 = 2

    num1 /= num2

    print(num1)

}

Output

3

Modulus Assign

fun main() {

    var num1 = 6

    var num2 = 4

    num1 %= num2

    print(num1)

}

Output

2

4. Increment and decrement operator

Increment or decrement operators are used to increase or decrease the values by 1. It is very helpful in the case of a loop. Because in the case of the loop. We want to self increment or decrement the variable.

It is further divided into two categories.

1. Pre

2. Post

Pre- Increment in Kotlin operators

Pre-Increment is used to first increase the value. Then show the incremented value.

Example

fun main() {

    var num = 6

    print(++num)

}

Output

7

Post- Increment in Kotlin operators

Post-Increment is used to first show the value. Then increment the value.

Example

fun main() {

    var num = 6

    print(num++)

}

Output

6

Pre- Decrement in Kotlin operators

Pre-Decrement is used to first decrease the value. Then show the decremented value.

Example

fun main() {

    var num = 6

    print(--num)

}

Output

5

Post- Decrement in Kotlin operators

Post-Decrement is used to first show the value. Then decrement the value.

Example

fun main() {

    var num = 6

    print(num--)

}

Result

6

Post a Comment