Kotlin Flow Control : If-Else Expression



If - Else Expression is a control flow mechanism available in the Kotlin Programming language. Similar to other functional language, 'if' is an expression, it is not a keyword in kotlin.

The expression 'if' will return a value whenever necessary. Like other programming language, 'if-else' block is used as an initial conditional checking operator.

Example:
fun main(args: Array<String>) {
   val a:Int = 10
   val b:Int = 5
   var max: Int
   
   if (a > b) {
      max = a
   } else {
      max = b
   }
   print("Max is :" +max)
 
   // As expression 
   // val max = if (a > b) a else b
}
Output : Max is : 10

Note:
You can replace entire If-Else block with  val max = if (a > b) a else b.

Comments