If else Statement in Kotlin

Definition

The if statement is used to check a condition. If the condition mentioned in the if the condition is true, then the block of code inside the if block is executed, otherwise the block of code inside the else part is executed. 

Syntax

if(condition){
	//Block of code
}else{
	//Block of code
}

If..else statement

These expressions can also be written as blocks as,

var num = 5
var maximum = 10
val max = if(num>maximum){
	num
} else {
	maximum
}

In the above statements, the condition is checked and the maximum value is assigned to the "max" variable.

if else Kotlin example program

// If Else Statement Kotlin example program
// Conditional Statements Kotlin Programs, Basic Kotlin Programs

fun main(args: Array < String > ) {

    val num = 5 // num = 10
    
    // If Condition Statement
    if (num == 5) {
        // If Block
        println("Condition of $num equal to 5 is : True ")
    } else {
        // Else Block
        println("Condition of $num equal to 5 is : False ")
    }
}

Sample output

//If num = 5
Condition of 5 equal to 5 is : True 

//If num = 10
Condition of 10 equal to 5 is : True