In the following tutorial, we’re going to understand how to use condtional if statements and uses of boolean type in a program written in Kotlin.
- This tutorial assumes, you have a basic working knowledge of IntelliJ Idea.
- Have Kotlin installed setup in your machine.
Represents a value which is either true or false. On the JVM, non-nullable values of this type are represented as values of the primitive type boolean.
Functions callable on Booleans
- and : Performs a logical and operation between this Boolean and the other one. Unlike the && operator, this function does not perform short-circuit evaluation. Both this and other will always be evaluated.
- compareTo : Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it’s less than other, or a positive number if it’s greater than other.
- equals : Indicates whether some other object is “equal” to this one.
- hashCode : Returns a hash code value for the object.
- not : Returns the inverse of this boolean.
- or : Performs a logical or operation between this Boolean and the other one. Unlike the || operator, this function does not perform short-circuit evaluation. Both this and other will always be evaluated.
- toString : Returns a string representation of the object.
- xor : Performs a logical xor operation between this Boolean and the other one.
if statements :
The if statement allows you to specify a section of code that is executed only if a given condition is true-
The curly braces are optional if the body of if statement contains a single line -
if(n % 2 == 0) println("$n is even.")
The if-else statement executes one section of code if the condition is true and the other if the condition is false.
Similar to if statement, the curly braces are optional for if-else if the body of the statement contains a single line -
if(n % 2 == 0) println("$n is even.") else println("$n is odd.")
if as an expression:
In Kotlin, one can use if as a statement as well as an expression i.e., you can assign the result of an if-else expression to a variable.
For Example -
When using if as an expression, it is compulsory to have an else branch, else the compiler will throw an error. The if-else branches can also have block of code. In case of block of code, the last expression is the value assigned to the variable being expressed -
12 is less than or equal to 25 max(12, 25) = 25
Unlike Java, Kotlin doesn’t have a ternary operator because we can easily achieve what ternary operator does, using a single line if-else expression.
if-else-if chain can be written like the below code, although there’s a elegant way to do it instead of chaining multiple if-else conditions. We’ll be looking at it next.
when - a replacement of switch and if-else-if chain:
If you’re familiar with Java, you must have come across switch statement. In Kotlin, we have a cleaner way to achieve that, as described in the below example:
when expression matches the given argument with all the branches one by one until a match is found. Once a match is found, it executes the matched branch. If none of the branches match, the else branch is executed by default.
when - as an expression:
Similar to if, when can be used as an expression as well.
In the next Kotlin article, we’ll be developing our first android app, written completely in Kotlin language. Stay tuned.
Keep coding! Have fun.