Industrial Training




Kotlin if Expression


In Kotlin, if is an expression is which returns a value. It is used for control the flow of program structure. There is various type of if expression in Kotlin.


  • if-else expression
  • if-else if-else ladder expression
  • nested if expression

Traditional if Statement


Syntax of traditional if statement


if(condation){  
//code statement  
}  
if(condation){  
//code statement  
}  

Syntax of traditional if else statement


if(condation){  
//code statement  
}  
else{  
//code statement  
}  
if(condation){  
//code statement  
}  
else{  
//code statement  
}  

Kotlin if-else Expression


As if is an expression it is not used as standalone, it is used with if-else expression and the result of an if-else expression is assign into a variable.


Syntax of if-else expression


val returnValue = if (condation) {  
     //code statement  
    } else {  
     // code statement  
    }  
    println(returnValue)  
val returnValue = if (condation) {  
     //code statement  
    } else {  
     // code statement  
    }  
    println(returnValue)  

Kotlin if-else Expression Example

Let's see an example of if-else expression.


fun main(args: Array< String>) {  
        val num1 = 10  
        val num2 =20  
        val result = if (num1 > num2) {  
            "$num1 is greater than $num2"  
        } else {  
            "$num1 is smaller than $num2"  
        }  
        println(result)  
}  
fun main(args: Array< String>) {  
        val num1 = 10  
        val num2 =20  
        val result = if (num1 > num2) {  
            "$num1 is greater than $num2"  
        } else {  
            "$num1 is smaller than $num2"  
        }  
        println(result)  
}  

Output:
10 is smaller than 20

We can remove the curly braces of if-else body by writing if expression in only one statement.


For example:
fun main(args: Array< String>) {  
        val num1 = 10  
        val num2 =20  
        val result = if (num1 > num2) "$num1 is greater than $num2" else "$num1 is smaller than $num2"  
        println(result)  
}  
fun main(args: Array< String>) {  
        val num1 = 10  
        val num2 =20  
        val result = if (num1 > num2) "$num1 is greater than $num2" else "$num1 is smaller than $num2"  
        println(result)  
}  		  

Using if-else expression in one single line statement is like ternary operator in Java. Kotlin does not support any ternary operator.


Kotlin if-else if-else Ladder Expression


Let's see an example of if-else if-else ladder expression.


fun main(args: Array< String>) {  
    val num = 10  
    val result = if (num > 0){  
        "$num is positive"  
    }else if(num < 0){  
        "$num is negative"  
    }else{  
        "$num is zero"  
    }  
    println(result)  
}  
fun main(args: Array< String>) {  
    val num = 10  
    val result = if (num > 0){  
        "$num is positive"  
    }else if(num < 0){  
        "$num is negative"  
    }else{  
        "$num is zero"  
    }  
    println(result)  
}  			  

Output:
10 is positive

Kotlin Nested if Expression


Let's see an example of nested if expression.


fun main(args: Array< String>) {  
    val num1 = 25  
    val num2 = 20  
    val num3 = 30  
    val result = if (num1 > num2){  
  
        val max = if(num1 > num3){  
            num1  
        }else{  
            num3  
        }  
        "body of if "+max  
    }else if(num2 > num3){  
        "body of else if"+num2  
    }else{  
        "body of else "+num3  
    }  
    println("$result")  
}  
fun main(args: Array< String>) {  
    val num1 = 25  
    val num2 = 20  
    val num3 = 30  
    val result = if (num1 > num2){  
  
        val max = if(num1 > num3){  
            num1  
        }else{  
            num3  
        }  
        "body of if "+max  
    }else if(num2 > num3){  
        "body of else if"+num2  
    }else{  
        "body of else "+num3  
    }  
    println("$result")  
}  			  

Output:
body of if 30


Hi I am Pluto.