If Statement in Kotlin

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