In this article you will learn how to handle conditional situations in programming.
Let’s say, we want to detect whether a given number is even or odd. If the number is even, we want to print “The number is even” else we want to print “The number is odd”. This behavior can be achieved by using the following code.
fun evenOrOdd(a: Int) {
if(a % 2 == 0) {
println("The number is Even")
} else {
println("The number is Odd")
}
}
As you can see, just like how we would write conditional statements in English we can represent it in programming using if/else statement.
When we have multiple conditions, we can illustrate them using if/else if…/else condition.
fun findRange(a: Int) {
if (a >= 0 && a < 10) {
println("The number is in ones")
} else if(a >= 10 && a < 99) {
println("The number is in tens")
} else if (a >= 100 && a < 999) {
println("The number is in hundreds")
} else if ( a >= 1000 && a < 9999) {
println("The number is in thousands")
} else {
println("The number is greater than or equal to 10 thousands")
}
}
In the code above, based on the range of a number, different statements are printed. We can rewrite the above code as shown below.
fun findRange(a: Int) {
if(a in 0..10) {
println("The number is in ones")
} else if(a in 10..99) {
println("The number is in tens")
} else if (a in 100..999) {
println("The number is in hundreds")
} else if (a in 1000..9999) {
println("The number is in thousands")
} else {
println("The number is greater than or equal to 10 thousands")
}
}
You can see that in line 2 we have a in 0..10, it is the same as writing a>=0 && a <=10.
Nested If/ else condition
We can also have conditional statements inside a conditional statement as shown in line 2 and 3.
fun findRange(a: Int) {
if (a >= 0 && a < 10) {
if (a < 20) {
println("The number is in teens")
} else {
println("The number is in tens")
}
} else if (a >= 100 && a < 999) {
println("The number is in hundreds")
} else if ( a >= 1000 && a < 9999) {
println("The number is in thousands")
} else {
println("The number is greater than or equal to 10 thousands")
}
}
When Statement
An alternative to classical if/else statement is to use when statement to represent conditions. The first problem of checking if the given number is even or odd can be written using when as shown in the fig. 5
fun evenOrOdd(a: Int) {
when {
a % 2 == 0 -> println("The number is Even")
else -> pritln("The number is Odd")
}
}
Let’s see another example where the ranges are used with when.
fun detectRange(a: Int) {
when {
a in 0..10 -> println("The number is in ones")
a in 10..99 -> println("The number is in tens")
a in 100..999 -> println("The number is in hundreds")
a in 1000..9999 -> println("The number is in thousands")
else -> println("The number is greater than or equal to 10 thousands")
}
}
We can rewrite the above when statement as:
fun detectRange(a: Int) {
when(a) {
in 0..10 -> println("The number is in ones")
in 10..99 -> println("The number is in tens")
in 100..999 -> println("The number is in hundreds")
in 1000..9999 -> println("The number is in thousands")
else -> println("The number is greater than or equal to 10 thousands")
}
}
I would prefer to use when statement instead of if/ else statement especially if there are multiple conditions as it makes the code more readable. Click on the next button to continue reading.
No responses yet