In this tutorial we are going to discuss various decision making statement in Kotlin. Decision making statement are also known as Control Statement that controls the flow of a program.
Kotlin supports following decision making statements
- if expression
- if else expression
- if, else-if ladder expression
- when statement
if expression
if statement is a one way branching statement. General syntax is given here
if (expression) statement
where expression results in a boolean value. If the expression evaluates to true, then statement will be executed, otherwise statement will be ignored and the next statement will be executed.
example:
fun main()
{
var a = 15
println("before if statement")
if(a>=18)
{
println("Hello, you are eligible to vote ")
}
println("after if statement")
}
in above example println() within if block is executed only when the value of a is greater than 18.
if-else expression
if-else is an two way branching statement. General syntax is given here
if (expression)
{
// statement when expression is true
}
else
{
// statement when expression is false
}
example
fun main()
{
var a = 15
println("before if-else statement")
if(a>=18)
{
println("Hello, you are eligible to vote ")
}
else
{
println("Sorry, you are not eligible to vote ")
}
println("after if-else statement")
}
if-else-if ladder
example
fun main()
{
var day = 1
if (day == 1) {
println("Today is Sunday")
}
else if (day == 2) {
println("Today is Monday")
}
else if ( day == 3) {
println("Today is Tuesday")
}
}
The existing feature about Kotlin’s if is that it’s an expression, which means we can do things like
fun main()
{
val que = "Who is sachin "
val ans = "Indian Cricketer"
val correctAns = ""
var msg = if (ans == correctAns)
{
"You are correct"
}
else
{
"Try again"
}
println (msg)
}
The String on the first block of the if construct will be returned to the message variable if the condition is true; otherwise, the String on the second block will be the returned value.
Another example of if-else as an expression to find maximum number
fun main()
{
var a = 15
var b = 20
var c = if(a >= b)
a
else
b
println ("Maximum number is $c")
}
when statement
Kotlin does not support switch statements like Java, instead it has the new feature for the same, that is when statement. Its functionality is almost similar to switch statements. It can be written as follow
fun main()
{
print("Enter Day Number: ")
var day = readLine()!!
when (day.toInt()) {
1 -> println("Sunday")
2 -> println("Monday")
3 -> println("Tuesday")
4 -> println("Wednesday")
5 -> println("Thursday")
6 -> println("Friday")
7 -> println("Saturday")
else -> println("Invalid day number")
}
}
when matches that argument day against all the options sequentially until it finds the match. Unlike Java, the break keyword is not required here to specify the end of a block because when a match is found it does not go through the next match by itself.
The when statement, like if statement can be used as an expression, when it is used like this it returns the value of expression like below.
fun main()
{
print("Enter Day Number: ")
var day = readLine()!!
var msg =
when (day.toInt()) {
1 -> "Sunday"
2 -> "Monday"
3 -> "Tuesday"
4 -> "Wednesday"
5 -> "Thursday"
6 -> "Friday"
7 -> "Saturday"
else -> "Invalid day number"
}
println(msg)
}
Do not forget to add the else clause when it is used as an expression. Because the compiler checks all possible paths and it needs to be complete, therefore, the else clause becomes important.
We are not restricted to numeric literal value, it can be of any other data type too. As shown in code below
fun main()
{
print("What is your answer to life? : ")
var response:Int? = readLine()?.toInt()
val message = when(response){
32 -> "Very good, and thanks for the all wish"
33, 34, 35 -> "either 33,34 or 35"
in 51 .. 100 -> "fifty one to one hundred"
else -> "Sorry ! It Not what I'm looking forward to"
}
println(message)
}
Note:
- In branch conditions we can have multiple values using commas.
- The range operator can be used to check the range of values.
- The else clause is mandatory when when is used as an expression