val v = 9 val result = if (v > 10) "Greater than 10" else "less than 10" println(result)
Less than 10
We can write conditional statements in one line as shown in the above code example. Here, we have created a variable named v and assigned a value to it. We are assigning value to variable named result based on some conditions. If v is greater than 10 it(result) will be assigned "Greater than 10" value else it will be assigned "Less than 10" value.
if (condition) a else b
Code example 2
val isUser = true val userRole = if (isUser) "user" else "ext" print(userRole)