The Programmers Guide To Kotlin - If and When
Written by Mike James   
Monday, 05 July 2021
Article Index
The Programmers Guide To Kotlin - If and When
The When
IF V When

Conditional execution is a key part of programming and Kotlin has if and when but where do you use which? This ia an extract from the second edition of my book on Kotlin for Programmers.

.

Programmer's Guide To Kotlin Second Edition

kotlin2e360

You can buy it from: Amazon

Contents

  1. What makes Kotlin Special
  2. The Basics:Variables,Primitive Types and Functions 
  3. Control
         Extract: If and When 
  4. Strings and Arrays
  5. The Class & The Object
  6. Inheritance
  7. The Type Hierarchy
  8. Generics
  9. Collections, Iterators, Sequences & Ranges
        Extract: Iterators & Sequences 
  10. Advanced functions 
  11. Anonymous, Lamdas & Inline Functions
  12. Data classes, enums and destructuring
        Extract: Destructuring 
  13. Exceptions, Annotations & Reflection
  14. Coroutines
        Extract: Coroutines 
  15. Working with Java
        Extract: Using Swing ***NEW!

<ASIN:B096MZY7JM>

All programming languages have to give you ways of modifying the flow of control - making loops, conditional execution and so on. Kotlin uses a fairly traditional approach, but there are some exceptionally nice touches that when used correctly can make your programs much easier to understand.

The If

The Kotlin if statement is very similar to the if statement in any modern language but with one small, beneficial, twist that we will discover later.

You can write an if statement in the usual way:

if ( condition ) statement

or you can use a compound statement to execute multiple statements:

if (condition) {
 statements
}

As always in Kotlin, a compound statement, a list of statements between curly brackets, is treated as a single statement. All Kotlin control statements work with single statements, and hence also work with compound statements.
For example you can write:

if(a>b) println(b)

or:

if(a>b) {
  println(a)
  println(b)
}

You can also use else to specify what happens when the condition is false:

if( condition ){
 statements
}else{
statements
}

For example:

if(a>b){
 println(a)
}else{
 println(b)
}

You can, of course, use the if statement to build more complicated conditionals by nesting multiple if...else statements.

For example:

if (a > b) {
        println(a)
    } else if (b > c) {
        println(b)
    } else {
        println(c)
    }

To be clear, there isn't a special elseif as there is in some other languages because you simply don't need it. Writing an if within an else clause gives you the same effect. The if will only be executed if the else clause is executed.

This sort of nested if statement is fine, but if you nest if and else clauses too deeply it becomes confusing.

How deep is too deep?

When the structure becomes confusing to another programmer.

A nested if selects what is to be done on the basis of a tree of conditions. That is, a nested if is exactly equivalent to a decision tree. The first condition is the root of the tree and each condition divides the tree into a true and a false branch; the true corresponds to the if and the false to the else. The executable clauses are the terminal nodes of the tree and which one is executed depends on the path taken through the tree to a node.

if

A tree is a very compact representation of a decision process, but this compactness makes it very difficult to understand its total meaning. You can follow one branch easily enough, but what about the others?

You can always convert a tree into a set of sequential ifs by taking each branch and writing it as a condition with a single if. The result is a list of if statements with a clear condition that determines when it should be executed. This is a less compact way to write a complex decision, but it is nearly always much clearer. Kotlin, like many other languages, has a construct that lets you write a sequential decision process very easily.



Last Updated ( Monday, 05 July 2021 )