Skip to main content

If Statement in Kotlin

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

If Statement Definition

  • In Kotlin, If conditional statement executes the block of statements based on the condition inside the braces similar to other programming languages. If the condition is true, then the block of code inside the if the block is executed otherwise the if block is skipped.
  • IF conditional statement is a feature of the programming language which performs different computations or operations depending on whether a programmer-specified boolean condition evaluates to true or false.

Syntax

if(condition){
	//Block of code
}

For example,

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

(OR)

var num = 5
var maximum = 10
if(num>maximum) maximum = num

If Statement Example Program

fun main(args: Array<String>){
	//Assigning a constant value to a variable
	val num = 5
	//Assigning a value to a varying variable
	var maximum = 10 
	if(num > maximum){
		maximum = num
	}
	println("The maximum value is : $maximum")

}

Sample Output

The maximum value is : 10

How It Works

This Kotlin program demonstrates If 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 Statement in Kotlin?
If Statement is demonstrated in this Kotlin tutorial with a complete, runnable example and its sample output.
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 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