Conditional Operator in Kotlin : An Alternative for Ternary Operator

Home » Kotlin » Conditional Operator in Kotlin : An Alternative for Ternary Operator

Kotlin is a versatile programming language packed with many modern features, such as lambdas, coroutines, and a powerful NPE (NullPointerException) control system. Unfortunately, Kotlin does not have a direct ternary operator or conditional operator like some other commonly used programming languages. However, we can achieve similar functionality using if-else conditional statements. Let’s explore how to use a conditional operator in Kotlin using if-else statements.

Conditional Operator in Kotlin : An Alternative for Ternary Operator

What is Conditional Operator?

The conditional operator, also known as the ternary operator, is an operator used in some programming languages to evaluate a condition and return different values based on the condition. It provides a concise way to write conditional expressions in a single line of code.

The conditional operator takes three operands: the condition, the expression to be evaluated if the condition is true, and the expression to be evaluated if the condition is false.

How to use conditional operator in Kotlin language?

Let’s use Conditional Expression

In Kotlin, the ternary operator syntax of condition ? then : else is not available. However, you can use the if statement as an expression instead.

Open the Kotlin Playground and write the following code:

fun main()
{
    val status = false
    val check = if (status) "closed" else "opened"
    println("My connection status: $check")
}

Output

My connection status: opened

The if-else expression to assign a value to the variable check.

The if statement checks the value of the status variable. If status is true, the expression evaluates to “closed“, and if status is false, it evaluates to “opened“.

The result is assigned to the check variable. Finally, we use the println() function to display the connection status message. We use string interpolation ($check) to insert the value of the check variable into the string.

Conditional Expression in Kotlin with Example

fun main()
{
 val number1 = readln().toInt()
 val number2 = readln().toInt()
 println(findBig(number1, number2))
}
fun findBig(a: Int, b:Int) = if(a > b) a else b

Output

100
32
100


Inside the main() function, two variables number1 and number2 are declared. These variables are used to store the input received from the user. readln().toInt() is used to read a line of input from the standard input (console) and convert it to an integer using the toInt() function. The first input value is assigned to number1, and the second input value is assigned to number2.

Next, the println() function is used to print the result of the findBig() function. The findBig() function takes in two integers, a and b, and returns the larger value using an if-else expression. If a is greater than b, the expression evaluates to a, otherwise it evaluates to b. The result is then printed to the console.

Finally, the program executes and waits for the user to input two numbers. Once the numbers are entered, the program finds the larger number using the findBig() function and prints the result to the console.

You may also like...