Kotlin if else Expression
In this tutorial, we will learn how to control the flow of execution in Kotlin using the if-else expressions. Using if-else statements to control the flow of execution is available in almost all the modern programming languages.
In most of the languages, if-else
block is used to select a condition based on an expression. If the if
expression returns true, if
block will be executed. Otherwise, the else
block will be executed. For example, if we want to know whether a student passed the examination or not using the score, we can write the following code:
fun main() {
val marks = 70
val result:String
if(marks > 33){
result = "Hurrah! You passed"
}
else{
result ="Sorry! You failed"
}
println(result)
}
Hurrah! You passed
Here, because the marks of the student is greater than 33, he has passed the examination.
But in Kotlin, if-else is not a statement, it is an expression. It means the if-else block can return a value with a slight syntax change.
Let's see the code below:
fun main() {
val marks = 70
val result:String
result = if(marks > 33) {
"Hurrah! You passed"
}
else{
"Sorry! You failed"
}
println(result)
}
Here, based on the condition, the value will be returned from the if-else block and will be assigned to the result
variable.
Note: While using the if-else as an expression, it is compulsory to have an else
block with if
block.
There are various types of if-else expression in Kotlin:
-
Simple if-else expression
-
if-else ladder
-
Nested if expression
Simple if-else expression
Here is a pictorial representation of how an if
statement works if used without an else
block:
Let's take a code example to see the simple if
expression in action:
fun main() {
val light = "red"
if(light == "red"){
print("Stop")
}
}
Stop
And if we have if
and else
both specified in the code, then:
We already saw an example of simple if-else expression above. We can simplify this expression even more:
fun main() {
val marks = 70
val result:String
result = if(marks > 33) "Hurrah! You passed" else "Sorry! You failed"
println(result)
}
Hurrah! You passed
We can remove the curly braces if we have a single line code in if
and else
block.
The if-else Ladder
If we have more than one condition to check, then we can use if else
ladder. Let us understand this with an example.
Suppose we want to assign the grade to a student based on his/her marks; we can use if else
ladder:
fun main() {
val marks = 70
val grade: String
grade = if(marks >= 91)
"A+"
else if(marks >= 81)
"A"
else if(marks >= 71)
"B+"
else if(marks >= 61)
"B"
else if(marks >= 51)
"C+"
else if(marks >= 41)
"C"
else if(marks >= 33)
"D"
else
"F"
println("Your grade is: $grade")
}
Your grade is: B
To make the above code example more interactive, you can take input from the user and then show the grade as output.
Nested if-else expression
When we have an if
expression inside another if
expression, it is known as nested if
expression. Let us understand this with the help of an example.
We will write a program to find the maximum of three numbers.
Algorithm example:
-
Input three numbers: num1, num2 and num3.
-
If num1 is greater than num2:
-
Else if num2 is greater than num3, num2 is maximum
-
Else num3 is maximum
This can be implemented as:
import java.util.*
fun main() {
val scanner = Scanner(System.`in`)
// Taking three numbers as input
println("Enter first number: ")
val num1 = scanner.nextInt()
println("Enter second number: ")
val num2 = scanner.nextInt()
println("Enter third number: ")
val num3 = scanner.nextInt()
val max = if(num1 > num2){
if(num1 > num3)
num1
else
num3
}
else if(num2 > num3)
num2
else num3
println("Maximum number is: $max")
}
Enter first number:
10
Enter second number:
20
Enter third number:
11
Maximum number is: 20
The first section of the program above is taking input of 3 numbers from the user and then we are comparing those numbers to find the maximum number out of the 3 numbers. For this, we use an if else
condition inside another if
condition, and this is nested if
expression.
There is no limit on the nesting. You can have as many if
condition inside one if
condition or can make a nesting chain.
Summary
In this tutorial, we discussed the if
and else
expression in Kotlin. In the next tutorial, we will discuss the when
expression in Kotlin.