EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Kotlin Control Statements

Decision Making in programming is similar to decision-making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of a program based on certain conditions. If the condition is true then it enters into the conditional block and executes the instructions. 
There are different types of if-else expressions in Kotlin: 

  • if expression
  • if-else expression
  • if-else-if ladder expression
  • nested if expression

If- Expression : Kotlin’s if can be used both as a statement and as an expression. When used as an expression, it returns a value.

Syntax:

if(condition) {

       // code to run if condition is true
}

Example:

fun main(args: Array<String>) {
	var a = 3
	if(a > 0){
		print("Yes,number is positive")
	}
}

Output:

Yes, number is positive

if-else-if ladder Expression: Kotlin’s if-else-if ladder can be used both as a statement and as an expression. When used as an expression, it returns a value.

Syntax:

if(Firstcondition) {
    // code to run if condition is true
}
else if(Secondcondition) {
    // code to run if condition is true
}
else{
}

Example:

import java.util.Scanner

fun main(args: Array<String>) {

	// create an object for scanner class
	val reader = Scanner(System.`in`)
	print("Enter any number: ")

	// read the next Integer value
	var num = reader.nextInt()
	var result = if ( num > 0){
		"$num is positive number"
	}
	else if( num < 0){
		"$num is negative number"
	}
	else{
		"$num is equal to zero"
	}
	println(result)

}

Output:

Enter any number: 12
12 is positive number

Enter any number: -11
-11 is negative number

Enter any number: 0
0 is zero

If-Else Expression: Kotlin’s if-else can be used both as a statement and as an expression. When used as an expression, it returns a value.

Syntax:

if(condition) {
        // code to run if condition is true
}
else {
       // code to run if condition is false
}

Example:

if(condition) {
        // code to run if condition is true
}
else {
       // code to run if condition is false
}

Output:

fun main(args: Array<String>) {
    var a = 5
    var b = 10
    if(a > b){
        print("Number 5 is larger than 10")
    }
    else{
        println("Number 10 is larger than 5")
    }
}

nested if expression: Kotlin’s if-else-if ladder can be used both as a statement and as an expression. When used as an expression, it returns a value.

Syntax:

if(condition1){
            // code 1
      if(condition2){
                  // code2
      }
}

Example:

import java.util.Scanner

fun main(args: Array<String>) {

	// create an object for scanner class
	val reader = Scanner(System.`in`)
	print("Enter three numbers: ")

	var num1 = reader.nextInt()
	var num2 = reader.nextInt()
	var num3 = reader.nextInt()

	var max = if ( num1 > num2) {
		if (num1 > num3) {
			"$num1 is the largest number"
		}
		else {
			"$num3 is the largest number"
		}
	}
	else if( num2 > num3){
		"$num2 is the largest number"
	}
	else{
		"$num3 is the largest number"
	}
	println(max)

}

Output:

Enter three numbers: 123 231 321
321 is the largest number
End of lesson.