Kotlin if else Expression

In this tutorial you will learn about kotlin if, else and nested if else expression.

The if expression is definitely one of those constructs which lets us simplify our task of determining conditions during a program flow. Or rather, for performing any decisions in the program we look up to the if statement first, before considering any other because of our sheer familiarity with the statement and efficiency of the same. The latter part however, by and large depends on our problem statement. The if statement hence, is an integral part of any programming language.

if else expression kotlin

Kotlin, however has a new modification to offer. In this language the choice is given to the programmer whether he has to use the if as a statement or as an expression. Yes, in Kotlin, rather than just being a branching statement; if can even be used to return any value i.e., it can be used as an expression, here’s how this works.

if else Expression

In contrast to the traditional usage of the if statement as in java, c++, c# where in the if statement simply lets us perform or execute a block of statement following its branching property; the if expression lets us work with a value. It is capable of returning a value too. Let us see how:

Traditional Usage:

if (condition){
	//expressions or statements to be executed
}

Example:

fun main(args: Array<String>){
	val a = 120
	val b = 20
	
	if (a > b){
		print("a is greater")
	}
}

Using if as Expression:

fun main(args: Array<String>){
    val a = 20
    val b = 30;
    
    val maximum = if (a > b){
        print ("choose a")
        a
    } else {
        print ("choose b")
        b
    }
}

Here, in this example, the value of b gets returned to the variable maximum.

Note:

  • When using if as expression (returning value or assigning to variable) then it must have else block.
  •  In case if or else block contain more than one statement then last statement is returned as value.
  • Curly braces are optional in case the block contain only one statement.

Nested if else Expression

The nested if else construct is just the if else construct coiled up into some other if else constructs. The number of if else constructs we want to include in some other if else construct is totally dependent on the programmer according to the program code requirements. Here is how this works:

Traditional Usage:

if (condition1){
	//statements 1a
	
	if (condition2){
		//statements 2a
	} else{
		//statements 2b
	}
}
else {
	//statements 1b
}

Example:

fun main(args: Array<String>){
    val a = 30
    val b = 40
    val c = 20
    
    if (a > b){
        if (a > c)
            print("a is greater")
        else
            print("c is greater")
    }
    else {
        if(b > c)
        	print("b is greater");
        else
        	print("c is greater")
    }
}

Output: b is greater

Using as Expression:

fun main(args: Array<String>){
    val a = 60
    val b = 40
    val c = 20
    
    val max = if (a > b){
        if (a > c)
            "a"
        else
        	"c"
    }
    else {
        if(b > c)
        	"b"
        else
        	"c"
    }
    
    print("$max is greater")
}

Output: a is greater

Therefore to conclude, we can say the usage of if  statement has been extended to an if expression in Kotlin language and that gives a new dimension to this language by means of which if can even be used to return values.

Mention in comments if you have queries regarding above tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *