Kotlin Operators - Arithmetic, Relational & Logical Operators


Just like any other programming language, Kotlin has different operators - arithmetic operators (+, -, *, /, %), relational operators (>, <, >=, <=, ==, !=) and logical operators (&&, ||, !). Let's understand them one by one -

Arithmetic Operators 

In your Maths class, you must have performed these operations - addition, subtraction, multiplication, and division. Similarly, when you want your computer to do these operations you need these same operators. For usage, refer to the below code snippet - 

    fun main() 
    {
    	val i = 13
    	val j = 2
		    
    	println( i + j ) // 15
    	println( i - j ) // 11
    	println( i * j ) // 26
    	println( i / j ) // 6
    	println( i.toFloat() / j ) // 6.5
    	println( i % j ) // 1
    }
  
Most of the operations are straightforward. 2 things that require attention - 
  • Division - if both the values are integers then the result will be an integer. (13/2 = 6). To get the actual output which is 6.5 in this case - you need to convert one of the values to a floating-point number. This is done by calling .toFloat() method i.e. 13.0/2 = 6.5
  •  Modulus Operator or Remainder Operator - This operator is used to calculate the remainder. When you divide 2 numbers, the remainder of this operation is calculated using % modulus operator i.e. 13%2 = 1 (when you divide 13/2 you get 6 as quotient and 1 as remainder).

Relational Operators

When you find out the relation between 2 values, you use these operators. Relations like greater than, less than, or equal to, etc. For usage, refer to the below code snippet - 

  fun main() 
  {
  	val i = 13
  	val j = 2
    
  	println( i > j ) // 13 > 2 = true
  	println( i < j ) // 13 < 2 = false
  	println( i >= j ) // 13 >= 2 = true
  	println( i <= j ) // 13 <= 2 = false
  	println( i == j ) // 13 == 2 = false
  	println( i != j ) // 13 != 2 = true
  }
  

Here also these operations are easy to understand. The only thing to notice here is == i.e. double equals operator. Double equals operator is used to checking the equality i.e. if 2 numbers are equal and a single equal operator is used for assignment. So when you say val x = 10 you are saying - assign value 10 to variable x whereas when you write x == y - it means you are comparing x and y whether they are equal or not.

Logical Operators

These operators are used when you want to test multiple conditions. For e.g. if x is divisible by 2 and x is divisible by 3 - here you are checking if both the conditions are true. For this scenario, you need AND && operator. Let's understand these operator one by one -

AND && operator
if both the conditions are true - result is true

OR || operator 
if any of the condition is true - result is true

AND && Operator Example

  fun main() 
  {
    val i = 12        
    val result = (i % 2 == 0) && (i % 3 == 0)
    println(result) //true
  }
  
Here we want to test whether the number is divisible by both 2 and 3. For this, we will be using && operator. Since the value is 12 and divisible by both 2 and 3 - result is true. If any of the conditions fails then the result will be false. For e.g. if we take the value 15 then the result is false because 15 is divisible by 3 but not 2.

OR || Operator Example

  fun main() 
  {
    val i = 15        
    val result = (i % 2 == 0) || (i % 3 == 0)
    println(result) //true
  }

Here we want to test if the number is divisible either by 2 or divisible by 3. For these scenarios, we use the OR || operator. In the case of the OR operator, the result is only false when both of the conditions are false.

NOT ! Operator Example

  fun main() 
  {
    println(!true) //false
    println(!false) //true 
  }
This simple operator is used to negate the values. When the condition is true, the result will be false and when the condition is false, the result will be true.

Short-Circuiting

For && AND operator and || operator - this is an important concept that needs to be understood. 
  • In the case of && AND operator, the result is only true when both of the conditions are true. Now when the program runs and if it turns out that the first condition is false, the runtime does not evaluate the 2nd condition because there is no point in evaluating the 2nd condition because the result will remain false no matter what's the output of 2nd condition.
  • Similarly for the || OR operator, if the first condition is true, 2nd condition is not evaluated because the result will be true.

That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.

Happy Learning !! Cheers From CheezyCode.

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example