Kotlin - if, if-else, if-else-if and nested if statements

In this tutorial, we will learn Kotlin - if, if-else, if-else-if and nested if statements with examples.

Kotlin - if statement example

The if statement tests the condition. It executes the if block if a condition is true.

Syntax

if(condition){  
//code to be executed  
}
Here, if statement may be a single statement or a compound statement enclosed in curly braces (that is, a block).
The statements get executed only when the given condition is true. If the condition is false then the statements inside the if statement body is completely ignored.
The below Kotlin program demonstrates the usage of if statement:
package net.javaguides.kotlin

fun main(args: Array < String > ) {

    var x = 10;
    var y = 20;
    if (x < y) {
        System.out.println("x is less than y");
    }
    x = x * 2;
    if (x == y) {
        System.out.println("x now equal to y");
    }
    x = x * 2;
    if (x > y) {
        System.out.println("x now greater than y");
    }
    // this won't display anything
    if (x == y) {
        System.out.println("you won't see this");
    }

}
Output:
x is less than y
x now equal to y
x now greater than y

Kotlin - if-else Statement Example

The if-else statement also tests the condition. It executes the if block if a condition is true otherwise else block, will be executed.

Syntax:

if(condition){  
     statement 1; //code if condition is true  
}else{  
     statement 2; //code if condition is false  
}  
Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is an expression that returns a boolean value.
The if-else works like this: If the condition is true, then statement1 is executed. Otherwise, the statement2 (if it exists) is executed.
The below Kotlin program demonstrates the usage of the if-else statement:
package net.javaguides.kotlin

fun main(args: Array < String > ) {

    var testscore = 76;
    var grade = ' ';

    if (testscore >= 90) {
        grade = 'A';
    } else if (testscore >= 80) {
        grade = 'B'
    } else if (testscore >= 70) {
        grade = 'C';
    } else if (testscore >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }
    System.out.println("Grade = " + grade);
}
Output:
Grade = C

Kotlin - if-else-if Statement Example

Syntax:

Syntax:

if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}  
else if(condition3){  
//code to be executed if condition3 is true  
}  
...  
else{  
//code to be executed if all the conditions are false  
}  
The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.
The below Kotlin program demonstrates the usage of the if-else-if statement:
package net.javaguides.kotlin

fun main(args: Array < String > ) {

    var age = 17
    if (age < 12) {
        println("Child")
    } else if (age in 12. .17) {
        println("Teen")
    } else if (age in 18. .21) {
        println("Young Adult")
    } else if (age in 22. .30) {
        println("Adult")
    } else if (age in 30. .50) {
        println("Middle Aged")
    } else {
        println("Old")
    }
}
Output:
Teen

Kotlin - Nested if Statement Example

When there is an if statement inside another if statement then it is called the nested if statement.

Syntax:

if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}

Example:

package net.javaguides.kotlin

fun main(args: Array < String > ) {

    var num = 50;
    if (num < 100) {
        System.out.println("number is less than 100");
        if (num == 50) {
            System.out.println("number equal to 50");
            if (num > 40) {
                System.out.println("number is greater than 40");
            }
        }
    }

}
Output:
number is less than 100
number equal to 50
number is greater than 40


Comments