Skip to main content

If-Else Statement in Kotlin

2 min read Updated June 30, 2026
Share:
On this page (7sections)

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 

How It Works

This Kotlin program demonstrates If-Else Statement. It first prepares the data it needs, then uses conditional logic to decide the result, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Use conditional statements to handle the different cases.
  3. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Frequently Asked Questions

What is If-Else Statement in Kotlin?
The if statement is used to check a condition.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice If-Else Statement?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.

Related Tutorials

Search tutorials