Is There Any Ternary Operator in Kotlin?
Well, there is no ternary operator in Kotlin. However, this programming language still offers us some ways to achieve the same functionality as the ternary operator in any other programming language. The proceeding sections of this article will throw light on two of the most commonly used methods of implementing the ternary operator functionality in the Kotlin programming language in Ubuntu 20.04.
Implementing the Ternary Functionality in Kotlin in Ubuntu 20.04:
Since there is no ternary operator in the Kotlin programming language to achieve similar functionality in Kotlin in Ubuntu 20.04, we will be making use of the following two methods:
Method # 1: Using the “if-else” Expression in Kotlin:
The first method of mimicking the ternary operator’s behavior in Kotlin uses the “if-else” expression. This method can be learned by checking out the two examples shared below:
Example # 1: Checking the Value of a Single Variable:
In this Kotlin program, we will be checking the value of a single variable by using the “if-else” expression. However, this expression will behave exactly like the ternary operator. The following Kotlin script implements this functionality:
In this Kotlin program, we have created a value named “option” within our “main()” function. Then, we have assigned to it an integer value “10”. We basically want to print a “yes” message if the value of the variable is “10”; otherwise, “no”. For doing that, we have created another value named “output” and have assigned to it the statement “if (option==10) ‘yes’ else ‘no’”. This statement will compare the value of the “option” variable with “10” and will assign “yes” to the “output” variable if the condition is true. Otherwise, it will assign “no” to the output variable. Then, we just print the value of the “output” variable.
The following command needs to be executed for compiling this program:
Then, the following command can be utilized for running this program:
The output of our Kotlin script is “yes”, as shown in the image below. It means that the condition that we have implemented within our script was true.
Example # 2: Comparing Two Variables With Each Other:
Now, we will implement another example for imitating the behavior of the ternary operator with the “if-else” expression in Kotlin. In this example, we will be comparing the values of two variables with each other as shown in the following Kotlin script:
In this example, we have defined two values named “a” and “b” and have assigned them to the two different integers, i.e., 10 and 20, respectively. We want to print “true” if the values of these variables are equal; otherwise, false. We have first created a value named “output” and have declared it to be of data type “Boolean”. Then, we have equalized it to the statement “if (a==b) true else false”.
The output of this Kotlin script is “false”, as shown in the following image because the values of our variables were not equal:
Method # 2: Using the “when” Expression in Kotlin:
This is just another method of implementing the same functionality as the ternary operator. However, this method makes use of the Kotlin “when” expression. The following two examples will demonstrate this method. However, these examples will implement the same functionality as discussed in the previous two examples.
Example # 1: Checking the Value of a Single Variable:
For using the “when” expression of Kotlin for checking the value of a single variable, you can write a similar Kotlin program like the one shown in the image below:
We have defined a value “option” and assigned an integer “10” in this program. Then, we have created another value, “output”, and have equalized it to the “when” expression that operates over the “option” variable. Inside this expression, we have two cases. If the value of the “option” variable is “10,” the first case will be run; else, the second case will be executed. Then, we have printed the value of the “output” variable on the terminal.
The output shown in the following image implies that the value of our “option” variable was “10”:
Example # 2: Comparing Two Variables With Each Other:
To compare the value of two variables with the Kotlin “when” expression while depicting the functionality of the ternary operator, you can implement the Kotlin script shown in the image below:
In this script, we have defined two different values, “a” and “b”, and have assigned to them the integers “10” and “20”. Next, we have created a value “output” and have assigned to it the “when” expression that operates over the statement “a==b”. If this statement is true, then the value assigned to the “output” variable will be “yes”; otherwise, it will be “no”. Finally, we have printed this output using a “println” statement.
The output shown in the following image implies that our two variables were not equal:
Conclusion:
With this article, we wanted to find out if the ternary operator exists in the Kotlin programming language or not. Since no such operator exists within the Kotlin programming language, we shared with you the two methods with the help of examples using which you easily mimic the behavior of the ternary operator in Kotlin. Using either of these methods will no longer need to use the ternary operator in Kotlin in Ubuntu 20.04. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.