Types of Decision Statements
There are five main types of decision statements in Kotlin. Let's see them one by one.
IF Statement
IF is used when we have to do some tasks if a condition is met.
Syntax:
if(condition){
do some work when the condition is true
}
We can run some code when the IF block satisfies the condition.
Let us try to understand with an example.
Suppose I need 2 litres of water every day to stay fit. If I drink less than 2 litres a day, I will become unhealthy.
fun main() {
val waterDrankTodayInLitres = 1.9
val minWaterRequiredInLitres = 2.0
if (waterDrankTodayInLitres < minWaterRequiredInLitres) {
println("Drink some more water")
}
}
We have the waterDrankTodayInLitres variable, which represents the water we drank today, and the minWaterRequiredInLitres variable, which means the minimum amount of water needed to stay healthy.
Here, as we can see, that condition is TRUE, and this code will print:
Drink some more water
IF ELSE Statement
IF ELSE is used when we have to handle code, the specified condition is not met.
Syntax:
if(condition){
do some work when condition is true
} else {
do something else as condition is false
}
We can run some code when the condition is satisfied in the IF block, but we can execute something else in the ELSE block if the situation is not met.
Let's add something more to the above example. I need 2 litres of water every day to stay fit. If I drink less than 2 litres a day, I will become unhealthy. Otherwise, I am healthy.
fun main() {
val waterDrankTodayInLitres = 2.9
val minWaterRequiredInLitres = 2.0
if (waterDrankTodayInLitres < minWaterRequiredInLitres) {
println("Drink some more water")
} else {
println("You have drunk enough water")
}
}
According to the code, the condition is false, and this code will print
You have drunk enough water.
IF ELSEIF ELSE Statement
IF ELSEIF ELSE is used to handle multiple edge cases without the need of writing different IF ELSE statements in the code.
Syntax:
if(condition1){
do some work when condition1 is true
} else if(condition2){
do some other work when condition2 is true
} else {
do something else when both condition 1 and 2 are false
}
We run some tasks according to a different condition, in a single IF ELSEIF ELSE block.
Let's add something more to the above example. I need 2 litres of water every day to stay fit. If I drink less than 2 litres a day, I will become unhealthy. If I drink exactly 2 litres a day, then I am healthy. Otherwise, I am over healthy.
fun main() {
val waterDrankTodayInLitres = 2.0
val minWaterRequiredInLitres = 2.0
if (waterDrankTodayInLitres < minWaterRequiredInLitres) {
println("Drink some more water")
} else if (waterDrankTodayInLitres == minWaterRequiredInLitres) {
println("You have drunk enough water")
} else {
println("You might have to spend your Quarter Day in the Bathroom")
}
}
As we can see that condition is TRUE, the code will print
You have drunk enough water
NESTED IF-ELSE Statement
NESTED IF ELSE is used if we have to handle multiple conditions if already some condition is met.
Syntax:
if(condition1){
do some work1
} else if(condition2){
if(condition2.1){
do something when condition 2 and 2.1 are met
} else {
do something when condition 2 is true but condition 2.1 is not
}
} else {
do something if condition 1 and 2 are false
}
We execute some code when some inner condition is satisfied.
We have two numbers, and we must check which number is more significant. Also, we have to check if the number is even or odd when both numbers are equal.
fun main() {
val x = 10
val y = 10
if (x > y) {
println("x is greater")
} else if (x < y) {
println("y is greater")
} else {
// we will check if x is even
if(x % 2 == 0) {
println("x is even and equal to y")
}
// x is odd
else {
println("x is odd and equal to y")
}
}
}
As x and y are equal, else block is executed.
Note: In the NESTED IF statement, we use modulo operator %, which gives the remainder between the two numbers. For more information, please head out to this article.
Output:
x is even and equal to y
WHEN Statement
WHEN is used when we have to handle multiple conditions and do not want to write too many if - elseif - else statements. We can have various expressions inside the WHEN block with numerous branches.
Syntax:
when (x) {
case1 -> fun1() // case1 is true then run fun1()
case2 -> fun2() // case2 is true then run fun2()
case3 -> fun3() // case3 is true then run fun3()
else -> some piece of code when none of the above conditions is satisfied.
}
Here x is the variable that will have some value, and according to that value, some function is executed.
Note: We have covered functions in great detail. Please refer to this article for more information.
Kotlin gives us some amazing features, so we don't have to write too much code. Before jumping to those features, first, let's analyze this code.
fun main() {
val x = 3
when (x) {
1 -> println("x is equal to 1") // x = 1
in 2..5 -> println("x lies between 2 and 5") // 2 <= x <= 5
6, 7 -> println("x is equal to 6 or 7") // x = 6 or x = 7
else -> println("x is more than 7") // x > 7
}
}
We have x equal to 3. We have passed this variable in the WHEN statement. If x = 1, then it will print x is equal to 1.
If x lies between 2 and 5, we will get x lies between 2 and 5 in the console. Here, we have used one of those mentioned features.
x..y means that value is in the range of 2 and 5 then return true
If x lies equal to 6 or 7, we will get x is equal to 6 or 7 in the output.
x,y,z means that value is 6 or 7 then return true else false
Finally, if none of the cases is TRUE, then we will get:
x is more than 7
Must Read Elvis Operator Kotlin
FAQs
-
What is a Control Flow Statement?
The program's execution is controlled by control flow. Control flow includes loops and decision statements. Control flow is a fundamental component of modern programming languages.
-
Is there any alternative to switch in Kotlin?
Yes, in Kotlin, Switch is replaced by when statement. In addition, we can use features like range or specific values in when statement. DEFAULT has been replaced to ELSE in the WHEN statement.
-
Why are Decision Statements so crucial in programming?
It will be nearly hard to develop a program if we do not use decision statements. Suppose we have to handle some edge case in our program then we have to use a decision statement.
Key Takeaways
Cheers if you reached here!!
The purpose of this article was to introduce you to the Kotlin Decision Statements, give some basic idea about these statements, and understand the need for Decision Statements in Programming.
If you want to learn more about Strings in Kotlin, then you can jump to this article which covers them in great detail.
However, learning never stops, and there is more to learn. So head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications. Till then, Happy Learning!