Kotlin Control Flow With Example

A Control Statement is a statement that checks other statements will be executed or not in control flow. There are many types of control statements but main types are FOR, WHILE, IF and CASE.

In this article, We are going to learn how each control statement is used in control flow in kotlin programming language.

 

If Expression

fun main(args: Array<string>) {

     val number1 = 10
     val number2 = 20

     if (number1 > number2)
         println(“Number1 is greater than Number2”)

}

 
 

If..Else Expression

if  (condition) {
     // codes to run if condition is true
}
else {
     // codes to run if condition is false
}
 
For One Statement:
fun main(args: Array<string>) {
     val number = -21
     val result = if (number > 0) “Positive number” else “Negative number”
     println(result)
}
 
For Multiple Statements:
fun main(args: Array<string>) {

     val number1 = 10
     val number2 = 20

     if (number1 > number2) {
         println(“Number1 is greater than Number2”)
         val difference = number1 – number2
     } else {
         println(“Number2 is greater than Number1”)
         val difference = number2 – number1
     }

}

 
 

If..Else IF..Else Expression

fun main(args: Array<String>) {

     val number = 0

     if (number > 0) {
         println(“Given Number is positive.”)
     } else if (number < 0){
         println(“Given Number is negative.”)
     } else {
         println(“Given Number is zero.”)
     }

}

 
 

Nested Expression

fun main(args: Array<String>) {

     val number = -21

     if (number == 0) {
         println(“Given Number is zero.”)
     } else {
         if (number < 0) {
             println(“Given Number is negative.”)
         } else {
             println(“Given Number is positive.”)
         }
     }

}

 
 

When Expression

As an Expression:
fun main(args: Array<String>) {

     val jerseyNumber = 7

     val playerName = when (jerseyNumber) {
         7 -> “Cristiano Ronaldo”
         8 -> “Stevan Gerrard”
         9 -> “Ronaldo”
         10 -> “Lionel Messi”
         else – > “Not Known”
     }

     println(playerName)

}

 
Without Expression:
fun main(args: Array<String>) {

     val jerseyNumber = 7

     when (jerseyNumber) {
         7 -> println(“Cristiano Ronaldo”)
         8 -> println(“Stevan Gerrard”)
         9 -> println(“Ronaldo”)
         10 -> println(“Lionel Messi”)
         else -> println(“Not Known”)
     }

}

 
For multiple statements:
fun main(args: Array<String>) {

     val jerseyNumber = 7

     when (jerseyNumber) {
         7 -> {
             println(“Cristiano Ronaldo”)
             println(“Player Country is Portugal.”)
         }
         10 -> {
             println(“Lionel Messi”)
             println(“Player Country is Argentina.”)
         }
         else -> println(“Not Known”)
     }

}

 
Check for multiple combined conditions:
fun main(args: Array<String>) {

     val dayOfWeek = 5

     when (dayOfWeek) {
         1,2,3,4,5 -> println(“Weekdays”)
         6,7 -> println(“Weekend”)
         else -> println(“Invalid”)
     }

}

 
Check in the range:
fun main(args: Array<String>) {

     val dayOfWeek = 5

     when (dayOfWeek) {
         1..5 -> println(“Weekdays”)
         6..7 -> println(“Weekend”)
         else -> println(“Invalid”)
     }

}

 
Check for particular type:
fun main(args: Array<String>) {

     val value = “hello”

     when (value) {
         is Int -> println(“value is Integer”)
         is String -> println(“value is String”)
         else -> println(“value type is not known”)
     }

}

 
 

While Loop

While loop repeats a specific block of code until certain condition is false.
 
fun main(args: Array<String>) {
     var i = 10
     while (i > 0) {
         i–
         print(“$i, “)
     }
}
 
 

Do…While Loop

The only difference between while and do…while loops in control flow is do…while loop is executed once before condition is checked.
 
fun main(args: Array<String>) {
     var i = 10
     do {
         i–
         print(“$i, “)
     } while (i > 0)

}

 
 

For Loop

For loop iterates through anything that provides an iterator.
 
fun main(args: Array<String>) {

     for (i in 1..3) {
         println(i)
     }
}

 
fun main(args: Array<String>) {

     for (i in 3..1) {
         println(i)
     }
}

 
fun main(args: Array<String>) {

     for (i in 1..10 step 2) {
         println(i)
     }
}

 
fun main(args: Array<String>) {

     var players = arrayOf(“Ronaldo”, “Messi”, “Neymar”, “Mbappe”)

     for (name in players) {
         println(name)
     }
}

 
fun main(args: Array<String>) {

     var players = arrayOf(“Ronaldo”, “Messi”, “Neymar”, “Mbappe”)

     for (pos in players.indices) {
         println(players[pos])
     }
}

 
fun main(args: Array<String>) {

     var players = arrayOf(“Ronaldo”, “Messi”, “Neymar”, “Mbappe”)

     for ((pos, name) in players.withIndex()) {
         println(“$pos: $name”)
     }
}

 
 

Break Expression

This expression is used to terminate a loop. It terminates the nearest enclosing loop.
 
fun main(args: Array<String>) {
     var i = 1;
     while (i > 0) {
         i++;
         if (i == 5) {
             break
         }
         println(i)
     }
}
 
fun main(args: Array<String>) {

     outer@ for (a in 1..5) {

         for (b in 1..2) {
             println(“a = $a; b = $b”)

             if (a == 2)
                 break@outer
         }
     }
}

 
 

Continue Expression

This expression is used to skip the current iteration of loop.

fun main(args: Array<String>) {
     for (i in 1..7) {
         if (i == 5) {
             continue
         }
         println(i)
     }
}
 
fun main(args: Array<String>) {

     outer@ for (a in 1..5) {

         for (b in 1..2) {
             if (a == 2)
                 continue@outer
         println(“a = $a; b = $b”)
         }

     }
}

 
 

Leave a Reply