Kotlin If-Else Expression and Statement


If-Else Statement

  • Humans have decision-making ability - they can take decisions based on the conditions. For e.g, if it is raining outside then it is better to take an umbrella. We want computers to make decisions based on the conditions and inputs we provide. This is where if-else comes into the picture. 
  • This is a control flow statement i.e. it decides the flow of the code based on the conditions. These are also known as conditional statements. In simple words, we say - 
            Me: Hey Computer!

            Computer: Yes.

            Me: Can you check if this Condition is true, if yes then do X thing else do Y.

            Computer: I am a good servant but please give proper instructions.

  • To write this type of code in Kotlin - we use an if-else statement. Let's see some examples - 
    if(booleanCondition)
    {
      // this code executes if the condition is true
    }
    else
    {
      // this code executes if the condition is false
    }
  
  • Boolean can be either true or false, based on the condition code is executed. Here is a small program that checks whether the number is even or odd - this is how we use the if-else statement in Kotlin.
    fun main() 
    {
   	val number = 5
   	if(number % 2 == 0)
   	{
   	    println("Number is Even")     
   	}
   	else
   	{
	    println("Number is Odd")             
   	}
    }
    
  • We can have multiple if-else conditions as well if you want to check multiple conditions. This is known as an else if ladder. Let's take one example - 
    fun main() 
    {
	val a = 13
	val b = 2
    	
	if(a > b)
	{
	  println("a is greater than b")
	}
	else if(a < b)
	{
	  println("a is less than b")
	}
	else
	{
	  println("a is equal to b")
	}
    }
  
  • You can nest if-else statements as well i.e. you can use if-else inside if and/or else statements. Refer to the below code snippet - 
    fun main()
    {
      if(condition1)
      {
       	  if(condition2){}
          else{}
      }
      else
      {
      
      }
   }	
  

If-Else Expression

  • In Kotlin, you can use if-else as an expression as well i.e. you can assign if-else to a variable. Result of the if-else is assigned to the variable. Above program which evaluates if the number is even or odd can be done using the if-else expression. Refer to the below snippet - 
      fun main() 
      {
        val number = 12
        val result = if(number % 2 == 0)
        {
            "Number is Even"    
        }
        else
        {
            "Number is Odd"
        }
        println(result)
     }
  
  • You can also remove the braces if you want so it will look something like this - 
val result = if(number %2 == 0) "Even" else "Odd"


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

Just Maths - Views, View Groups and Layouts

Create Android Apps - Getting Started