In this post, we will learn how to use kotlin if else block. There are 2 ways to use kotlin if else block –
a. Traditional way.
b. As an Expression.
a. Traditional way to use kotlin if else block
You can use if else block as an statement. This is traditional way to use if else block.
Syntax to use kotlin if else block as an statement is
if(testCondiction) // Code to perform some action when testCondition is true.
or
if(testCondition) // Code to perform some action when testCondition is true else // Code to perform some other action when testCondition is false
It means if statement can be followed by optional else statement.
– Example,
fun main(args: Array<String>) { var a = 20 var b = 30 var max = a if (a < b) max = b println("max = $max") }
When the above code is run, you will get below output
max = 30
fun main(args: Array<String>) { var a = 20 var b = 30 if (a < b) { a = a * 2 println(a) } else { b = b * 2 println(b) } }
When the above code is run, you will get below output
40
Since a is smaller than b, a will be multiplied by 2.
Important points to keep in mind while using kotlin if else block as an statement
a. If block may have zero or more else block conditions. You can also only use if block.
b. If block may have zero or more else if block conditions. i.e. You can only use if block or there may be if else if else….. else block conditions.
c. If any of the if block or else if block conditions succeeds, then, remaining conditions will not be tested.
– Example
fun main(args: Array<String>) { var a = 40 if (a < 10) { println("a is smaller than 10") } else if(a < 20) { println("a is smaller than 20") } else if(a < 30) { println("a is smaller than 30") } else if(a < 40) { println("a is smaller than 40") } else if(a < 50) { println("a is smaller than 50") } else if(a < 60) { println("a is smaller than 60") } else { println("a is greater than or equal to 60") } }
When you run the above program, you will get below output
a is smaller than 50
At fist, a < 10 will be tested. If it fails, then, a < 20, then, a < 30..and so on. Conditions upto a < 50 will be tested. Since a < 50 condition will succeed, remaining test conditions will not be tested.
b. Use Kotlin If Else block As an Expression
Unlike Java or any other programming language, you can also use If else block as an expression in kotlin. It means it returns a value. When you are using if as an expression, you must have else statement.
Syntax to use kotlin if else block as an expression is –
if(condition1) { // Code to perform some actions when condition1 is true.. // Last statement is the value to be returned when condition1 is true… } else { // Code to perform when condition1 is false… // Last statement is the value to be returned when condition1 is false… }
– Example,
fun main(args: Array<String>) { var a = 20 var b = 30 val max = if (a > b) { a } else { b } println("max = $max") }
When you run the program, you will get below output:
max = 30
NOTE: The curly brackets are optional if there is only one statement to be executed within condition. For example,
Statement 1: val max = if (a > b) { a } else { b } Statement 2: val max = if (a > b) a else b Here, Statement 1 and Statement 2 are same.
Did you know?
There is NO TERNARY OPERATOR in kotlin. This is because you can use if else block as an expression to fulfil the task done by TERNARY OPERATOR.
fun main(args: Array<String>) { var a = 20 var b = 30 val max = if (a > b) a else b println("max = $max") }
2. Guess the output of Below programs
fun main(args: Array<String>) { var a = 20 var b = 30 val max = if (a > b) { println("a is greater than b") a } else { println("a is not greater than b") b } println("max = $max") }
Kotlin If…else…if block ladder
You can use multiple if else if block as ladder in kotlin. For example,
fun main(args: Array<String>) { var a = 100 if (a < 10) { println("a is smaller than 10") } else if(a < 20) { println("a is smaller than 20") } else if(a < 30) { println("a is smaller than 30") } else if(a < 40) { println("a is smaller than 40") } else if(a < 50) { println("a is smaller than 50") } else if(a < 60) { println("a is smaller than 60") } else { println("a is greater than or equal to 60") } }
Output:
a is greater than or equal to 60
Kotlin Nested If else Block
if else block can be nested in kotlin. It means an if else block can be written inside another if else block.
fun main(args: Array<String>) { val a = 30 val b = 50 val c = -20 val max = if (a > b) { if (a > c) a else c } else { if (b > c) b else c } println("max = $max") }
Output:
max = 50
Explanation: This program calculates maximum value between a, b and c.
Conclusion
In this post, we have seen how to use kotlin if else block. Now, we clearly know that it can be used in 2 ways, Traditional way and as an Expression. In traditional way, we use it as statement. Since we can use kotlin if else block as expression, there is no ternary operator in kotlin.
Now, what next?
In the next post, you will learn about for block.