Kotlin If-Else

Kotlin If-Else

Kotlin if-else is a conditional statement, which can be used to execute a block of code based on a condition.

There are three variations of if-else based on whether to include else block, of chain multiple if-else statements.

if statement

The syntax of an if-statement is

if (condition) {
  // body
}

where

  • if is keyword.
  • ( is the syntax to define the start of condition.
  • condition is an expression that evaluates to a boolean value.
  • ) is the syntax to define the end of condition.
  • { denotes the start of the body for if-block.
  • } denotes the end of the body for if-block.

Explanation

If the condition is true, then the body of if-statement is executed. If the condition is false, then the body of if-statement is not executed.

Example – Check if given number is even

In the following program, we take a number in num, and check if the given number is even number or not using a condition, and print that the number is even, if it is so.

Kotlin Program

fun main(args: Array<String>) {
    val num = 16
    if (num % 2 == 0) {
        println("The number is even.")
    }
}

Output

The number is even.

Since the condition evaluates to true, the println() statement has been executed.

Now, let us take a number in num, such that the number is not even.

Kotlin Program

fun main(args: Array<String>) {
    val num = 11
    if (num % 2 == 0) {
        println("The number is even.")
    }
}

Output

Since the condition evaluates to false, the println() statement has not been executed.

if-else statement

The syntax of an if-else statement is

if (condition) {
  // if-block body
} else {
  // else-block body
}

where

  • if is keyword.
  • ( is the syntax to define the start of condition.
  • condition is an expression that evaluates to a boolean value.
  • ) is the syntax to define the end of condition.
  • { denotes the start of the body for if-block or else-block.
  • } denotes the end of the body for if-block or else-block.
  • else is keyword.

Explanation

If the condition is true, then the if-block body is executed. If the condition is false, then the else-block body is executed.

Example – Check if given number is even or odd

In the following program, we take a number in num, and check if the given number is even number or not using a condition, and print that the result to output.

Kotlin Program

fun main(args: Array<String>) {
    val num = 16
    if (num % 2 == 0) {
        println("The number is even.")
    } else {
        println("The number is not even.")
    }
}

Output

The number is even.

Since the condition evaluates to true, the println() statement in if-block has been executed.

Now, let us take a number in num, such that the number is not even.

Kotlin Program

fun main(args: Array<String>) {
    val num = 11
    if (num % 2 == 0) {
        println("The number is even.")
    } else {
        println("The number is not even.")
    }
}

Output

The number is not even.

Since the condition evaluates to false, the println() statement in else-block has been executed.

else-if statement

The syntax of an if-else statement is

if (condition1) {
  // if-block body
} else if (condition3) {
  // else-if block 1 body
} else if (condition2) {
  // else-if block 2 body
} else {
  // else-block body
}

where

  • if is keyword.
  • ( is the syntax to define the start of condition.
  • condition1, condition2, … are expressions that evaluates to a boolean value.
  • ) is the syntax to define the end of condition.
  • { denotes the start of the body for if block, else if block, or else block.
  • } denotes the end of the body for a block.
  • else is keyword.

Explanation

If the condition1 is true, then the if-block body is executed. If the condition1 is false, then the next condition i.e., condition2 is evaluated. If the condition2 is true, then the respective block is executed. But, if condition2 is false, it check the next condition if any.

If no condition is true, then else block is executed.

Example – Check if given number is positive, negative, or zero

In the following program, we take a number in num, and check if the given number is positive, negative, or zero, and print the output.

Kotlin Program

fun main(args: Array<String>) {
    val num = 14
    if (num > 0) {
        println("The number is positive.")
    } else if (num < 0){
        println("The number is negative.")
    } else {
        println("The number is zero.")
    }
}

Output

The number is positive.

Since the first condition evaluates to true, the println() statement in if-block has been executed.

You can change the value in num to a negative value, or zero, and observe the output.