Kotlin Tutorial 6 — Control Flow

Nick Apperley
3 min readDec 23, 2016

--

In this tutorial program control flow will be covered which includes conditional statements and loops. There will even be a section in the tutorial dedicated to handling nulls which is a very important topic.

Conditional Statements

Many programs will control flow in a program using the if statement and Kotlin is no exception. Try the following in the Kotlin REPL:

Kotlin unlike Python treats the if statement as an expression. When using the if statement as an expression it must be exhaustive otherwise a compile time error occurs. To see this in action try the following in the Kotlin REPL:

Moving onto the more advanced when statement which like the if statement can be treated as an expression. Python doesn’t have anything similar to the when statement. Each case (similar to an if/else if/else) in the when statement is executed sequentially (only one case that matches the condition is executed) from top to bottom. Try the following in the Kotlin REPL:

One other important thing to mention is a neat feature called Smart Casts. With Smart Casts a check can be made to see if a variable/constant is of a particular data type. If it is then a cast is automatically made to the data type used in the check. Try the following in the Kotlin REPL:

Handling Nulls

Kotlin has various ways of dealing will nulls which as Tony Hoare puts it, “I call it my billion-dollar mistake”. That quote is likely to be in the running for the top overused quote in 2016. Below is what would happen if you try to access a function/property off a variable/constant that uses a nullable data type:

Illustration 1: Accessing A Nullable Constant In Kotlin REPL

In order to fix the problem the ?. (Elvis) operator will be used to attempt a safe call off a nullable variable/constant. Try the following in the Kotlin REPL:

You can avoid using the Elvis operator by using an if or when statement with a basic a null check. If the check passes then a Smart Cast is made. Try the following in the Kotlin REPL:

Another way to handle a nullable variable/constant is to use the ?: (Else If Null) operator. Try the following in the Kotlin REPL:

Loops

Only three loops are supported: for, while, and do-while. Let us start with the easiest loop (for). Try the following in the Kotlin REPL:

Here is a for loop iterating through a Map (using Destructuring):

Now onto the final two loops: while, and do-while. Try the following in the Kotlin REPL:

Conclusion

Now you have all the key knowledge required to do control flow in a program, including handling nulls. Next tutorial covers basic code reuse and program decomposition with a bit of fun (Can you get the pun?) in Kotlin Tutorial 7 — Functions.

TOC

  1. Kotlin Tutorial 1 — Introduction
  2. Kotlin Tutorial 2 — Basic Data Types
  3. Kotlin Tutorial 3 — Basic Operators
  4. Kotlin Tutorial 4 — Handling Input And Output
  5. Kotlin Tutorial 5 — Basic Collections
  6. Kotlin Tutorial 6 — Control Flow
  7. Kotlin Tutorial 7 — Functions
  8. Kotlin Tutorial 8 — Modularity
  9. Kotlin Tutorial 9 — Using An IDE
  10. Kotlin Tutorial 10 — Classes
  11. Kotlin Tutorial 11 — Inheritance
  12. Kotlin Tutorial 12 — Encapsulation And Polymorphism
  13. Kotlin Tutorial 13 — Generics

--

--

No responses yet