Conditions with `if`
Learn how to use Kotlin's `if` conditions as well as comparison and logical operators.
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:
Press + to interact
Kotlin
if (planet == "Jupiter") {println("Radius of Jupiter is 69,911km")} else if (planet == "Saturn") {println("Radius of Saturn is 58,232km")} else {println("No data for planet $planet")}
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 ...
Ask