Making decisions in Kotlin
Our Kotlin code will constantly be making decisions. For example, we might need to know whether the user has new messages, or whether they have a certain number of friends. We need to be able to test our variables to see whether they meet certain conditions, and then execute a specific section of code depending upon whether they did or not.
In this section, as our code gets more in-depth, it helps to present the code in a way that makes it more readable. Let's take a look at code indenting to make our discussion about making decisions easier.
Indenting code for clarity
You have probably noticed that the Kotlin code in our project is indented. For example, the first line of code inside the MainActivity
class is indented by one tab. Additionally, the first line of code is indented inside each function by another tab; here is an annotated diagram to make this clear:
Notice that when the indented block has ended, often with a closing curly brace (}
), it is indented...