Conditions with `if`
Explore Kotlin's conditional control flow by learning how to use if statements effectively. Understand how to apply comparison and logical operators, how Kotlin handles equality differently from Java, and how to write clear, concise conditional code. This lesson prepares you to write complex conditions and sets the foundation for using when statements.
We'll cover the following...
For conditional control flow, Kotlin uses the if and when keywords. The syntax of if-blocks is the same as in C and Java. Similarly, the when keyword is similar to switch in these languages but more powerful, as you will learn.
Conditions using if #
Conditions with if can be written as follows:
Each condition is followed by a block of code in curly braces. There can be any number of else-if blocks (including zero) and up to one else-block.
Note: In the else-block, the planet variable is included in the output string using string interpolation. To do so, you just ...