if statement in Kotlin

In Kotlin, the if statement is used for conditional branching. It is similar to the if statement in other programming languages, but it has some additional features that make it more powerful and flexible.

The basic syntax of the if statement in Kotlin is as follows:

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

Here, condition is a boolean expression that determines whether the code inside the if block or the else block will be executed. If condition is true, the code inside the if block will be executed, otherwise the code inside the else block will be executed.

fun main() {
    val age = 25

    if (age >= 18) {
        println("You are an adult")
    } else {
        println("You are a minor")
    }
}

In this example, we have a variable age that contains a value of 25. We use the if statement to check whether age is greater than or equal to 18. If it is, the code inside the first block will be executed, which prints out the message “You are an adult”. If age is less than 18, the code inside the second block will be executed, which prints out the message “You are a minor”. In this case, since age is 25 which is greater than 18, the message “You are an adult” will be printed to the console.