Using Operators in Kotlin

Tutorial 4 of 5

Using Operators in Kotlin

Introduction

In this tutorial, our goal is to understand and explore the different types of operators in Kotlin and their usage. By the end of this tutorial, you will understand how to use various operators in Kotlin to create expressions and build complex programming logic.

Prerequisites:
- Basics of Kotlin Programming
- Basic understanding of programming concepts like variables, data types, etc.

Step-by-Step Guide

Operators are special symbols or keywords that are used to perform specific operations. Kotlin supports a rich set of operators, we will focus on Arithmetic, Relational, Assignment, Unary and Logical operators.

  • Arithmetic Operators: Used for basic mathematical operations.
  • Relational Operators: Used to compare two values.
  • Assignment Operators: Used to assign a value to a variable.
  • Unary Operators: Used to increment or decrement a value.
  • Logical Operators: Used to perform logical (AND, OR, NOT) operations.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.

val a = 10
val b = 20

println(a + b) // prints 30 - Addition
println(a - b) // prints -10 - Subtraction
println(a * b) // prints 200 - Multiplication
println(a / b) // prints 0 - Division
println(a % b) // prints 10 - Modulus

Relational Operators

Relational operators are used to compare two values. They return a boolean value - either true or false.

val a = 10
val b = 20

println(a == b) // prints false - Equality check
println(a != b) // prints true - Non-equality check
println(a > b) // prints false - Greater than check
println(a < b) // prints true - Less than check
println(a >= b) // prints false - Greater than or equal to check
println(a <= b) // prints true - Less than or equal to check

Assignment Operators

Assignment operators are used to assign a value to a variable.

var a = 10

a += 20 // Equivalent to a = a + 20
println(a) // prints 30

a -= 10 // Equivalent to a = a - 10
println(a) // prints 20

a *= 2 // Equivalent to a = a * 2
println(a) // prints 40

a /= 4 // Equivalent to a = a / 4
println(a) // prints 10

a %= 3 // Equivalent to a = a % 3
println(a) // prints 1

Unary Operators

Unary operators are used to increment or decrement a value.

var a = 10

a++ // Increment operator
println(a) // prints 11

a-- // Decrement operator
println(a) // prints 10

Logical Operators

Logical operators are used to perform logical operations such as AND, OR, and NOT.

val a = true
val b = false

println(a && b) // prints false - Logical AND
println(a || b) // prints true - Logical OR
println(!a) // prints false - Logical NOT

Summary

In this tutorial, we learned about various Kotlin operators - Arithmetic, Relational, Assignment, Unary, and Logical. We saw how to use them to create expressions and build complex programming logic.

For further learning, you can explore other Kotlin concepts like control flow statements, functions, classes, etc.

Practice Exercises

  1. Exercise 1: Write a program to add two numbers, subtract them, multiply them and divide them using arithmetic operators.
  2. Exercise 2: Write a program to compare two numbers using relational operators.
  3. Exercise 3: Write a program to perform logical operations using logical operators.

Solutions

// Solution for Exercise 1
val num1 = 10
val num2 = 20
println(num1 + num2) // prints 30
println(num1 - num2) // prints -10
println(num1 * num2) // prints 200
println(num1 / num2) // prints 0

// Solution for Exercise 2
println(num1 == num2) // prints false
println(num1 != num2) // prints true
println(num1 > num2) // prints false
println(num1 < num2) // prints true

// Solution for Exercise 3
val condition1 = true
val condition2 = false
println(condition1 && condition2) // prints false
println(condition1 || condition2) // prints true
println(!condition1) // prints false

After practicing these exercises, try to create more complex expressions using the operators you've learned.