Kotlin Programming – Control Flow

In this tutorial you’ll learn about control flow in Kotlin. To follow along, you can make use of the Kotlin – Playground.

Control Flow

Kotlin provides the following control flow statements:

  • for-in loop
  • while loop
  • if
  • when

For-in Loop

For-in loop is used to iterate over lists, maps, sets, ranges of numbers or characters in a string.

Iterating over a list:

val groceries = listOf("Fruits", "Yogurt", "Bread", "Vegetables")
 
for (item in groceries) {
    println("item is $item")
}

Iterating over a map:

val learnAlphabets = mapOf("a" to "apples", "b" to "bananas", "c" to "cherries", "d" to "dragon fruit")
 
for ((alphabet, fruit) in learnAlphabets) {
    println("$alphabet for $fruit")
}

Iterating over a range of numbers:

val rangeOneToFour = 1..4
 
for (i in rangeOneToFour) {
    println("$i x 10 = ${i * 10}")
}

Iterating over characters in a string:

val myName = "Zakhia"
 
for (character in myName) {
    println("Character is $character")
}

While Loop

You provide a condition in a while loop, whereby iterations keep on going until the condition becomes false. There are two kinds of while loop in Kotlin:

  • while
  • do-while
var counter = 0

println("While loop...")

// while loop
while (counter < 10) {
    counter++
    println(counter)
}
 
println("Do-while loop...")

// do-while loop
do {
    counter++
    println(counter)
} while (counter < 10)

The difference between a while loop and a do-while loop is that a while loop checks its condition before executing codes whilst the do-while loop checks its condition only after performing the first iteration.

Conditional Statement

Sometimes you might want to execute some codes only if a condition is true. Let’s say if the weather is sunny then you will go to the beach.

var isSunny = true
 
if (isSunny) {
    println("I will go to the beach")
}

Now let’s say you will do something else if the weather is not sunny.

isSunny = false
 
if (isSunny) {
    println("I will go to the beach")
} else {
    println("I will watch a movie on Netflix")
}

Now let’s say if the weather is sunny then you will go to the beach, if it’s not sunny but rainy, you will watch a movie on Netflix and if it’s not sunny nor rainy, you will go out with your friend.

val isRainy = false
 
if (isSunny) {
    println("I will go to the beach")
} else if (!isSunny && !isRainy) {
    println("I will go out with my friend")
} else {
    println("I will watch a movie on Netflix")
}

When

When is an alternative way to the if statement for executing codes based on some conditions. A when statement must be exhaustive, which means that either you provide all the values possible or only some values with an else.

val output = 15 * 3
val myGuess = 45
 
when (myGuess) {
    40 -> println("Guess again")
    45 -> println("Correct answer")
    else -> println("Wrong answer")
}

You can provide multiple values separated by commas to execute the same piece of codes.

val name = "zakhia"
 
when (name) {
    "Zakhia", "zakhia", "ZAKHIA" -> println("That's my name!")
    else -> println("Not my name")
}

Reference

https://kotlinlang.org/docs/reference/control-flow.html

Advertisement