Conditional truth is part of computer languages. This is usually some variation of “if x > y: [do some code]” Obviously this is involved in the logic and decision paths of a program. Conditions are evaluated for truth, and if found true then some specific path is executed. In Java this syntax follows like so:

if(x>y){
     System.out.println("x is greater than y");
} else {
     System.out.println("x is not greater than y");

Sometimes our if statements can become very complex. We may be checking for 8 or more evaluations. That would be very messy if we wrote if [condition 1], else if [condition 2], else if [condition 3]. To help with this, there’s the idea of Switch statements. Switches take a variable that has many conditions, and allows to developer to set a return based upon each case:

switch(x):
     case 0: 
          System.out.println("....");
          break;
     case 1:
          System.out.println("....1");
          break;
     case 2: 
          x = x+1;
          System.out.println(x);
          break;
     default:
          System.out.println("try again");

Kotlin When

Kotlin uses a different syntax that has some very interesting options. In Kotlin this is called the when statement, which looks like this:

when(age) {
    0 -> "Invalid age"
    in 1..12 -> "You need your parents supervision"
    in 13..16 -> {
          "You can see all G, PG and PG 13 movies"
          "You can not see R or M rated movies"
    }
    17, 18 -> "Looks like you're an adult, go right on in."
    in 65..100 -> "You get the senior discount on tickets"
    !in 101..Int.MAX_VALUE -> "Ok, no way you're that old"
    else -> "Input a valid age."

There’s a lot of conditions there and it reads a lot easier. We don’t have to run through 100 conditions on age. We can use ranges, like if an age is between 1 and 12, or 13 and 16. We can also specify multiple values, such as if the age is 17 or 18. We can even do a NOT in, which in this case above is checking if the age is above 101 (to max value of INT) we return another response.

Kotlin Smart Casts

Kotlin also offers something in the way of a “smart cast.”1 We can do an valuation within the evaluation… like so:

when(age) {
    is Int -> print(age)
    is String -> print("Please enter a number for your age")
}

When Assignment

Another interesting use case, is assigning the result of the when check to a variable. Consider this example 2:

var result = when(age) {
    is Int -> age + 20
}

When Function Evaluation

Another trick from the Udemy “Kotlin Course,” is that When statements can evaluate a function on the left for truth. Check out this example from the course:

when(x){
     "hey there".length -> "x is the length of the string 'hey there'"
     3 * 12 -> "x is equal to the value of 3*12"
}

If x is 9, then it will trigger on the length of “hey there” which is also 9, and it will output “X is the length of the string ‘hey there'”.


Update 6/2022: I would also like to link to a more in depth tutorial on Kotlin over at Guru99: https://www.guru99.com/kotlin-tutorial.html

FOOTNOTES


https://www.guru99.com/kotlin-tutorial.html

  1. From https://superkotlin.com/kotlin-when-statement/
  2. From https://www.udemy.com/kotlin-course
#

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Archives
Categories