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