Kotlin Control Flow Tutorial: if-else, when Expression, Loops, break, continue, and return


This Kotlin Control Flow tutorial explains how program execution is controlled using conditional statements and loops. It covers if-else conditions, the when expression, for loops, while and do-while loops, and control statements such as break, continue, and return. Each topic includes clear syntax, practical examples, and best practices to help learners write readable, efficient, and well-structured Kotlin programs.

Kotlin Control Flow – Complete Tutorial

if and else

The if statement is used to execute code based on a condition. In Kotlin, if can be used as both a statement and an expression.

Example


val age = 18

if (age >= 18) {
println("Eligible to vote")
} else {
println("Not eligible to vote")
}

if as an Expression


val result = if (age >= 18) "Adult" else "Minor"
println(result)

Best Practices

  1. Use if expressions to reduce code length.
  2. Avoid deeply nested if-else blocks.

when Expression

The when expression is a powerful replacement for switch statements in other languages.

Example


val day = 3

when (day) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
else -> println("Invalid day")
}

when as an Expression


val grade = 'A'

val result = when (grade) {
'A' -> "Excellent"
'B' -> "Good"
'C' -> "Average"
else -> "Fail"
}

Best Practices

  1. Use when instead of multiple if-else statements.
  2. Always include an else branch when required.

for Loop

The for loop is used to iterate over ranges, arrays, and collections.

Example with Range


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

Example with Collection


val languages = listOf("Kotlin", "Java", "Python")

for (lang in languages) {
println(lang)
}

Best Practices

  1. Use meaningful loop variable names.
  2. Prefer for-each loops for collections.

while and do-while Loops

while Loop

Executes as long as the condition is true.


var count = 1

while (count <= 5) {
println(count)
count++
}

do-while Loop

Executes at least once before checking the condition.


var number = 1

do {
println(number)
number++
} while (number <= 5)

Best Practices

  1. Use while when the number of iterations is unknown.
  2. Avoid infinite loops by ensuring condition updates.

break, continue, and return

break

Terminates the loop.


for (i in 1..5) {
if (i == 3) break
println(i)
}

continue

Skips the current iteration.


for (i in 1..5) {
if (i == 3) continue
println(i)
}

return

Exits a function and optionally returns a value.


fun checkNumber(num: Int): String {
if (num < 0) {
return "Negative"
}
return "Positive"
}

Best Practices

  1. Use break and continue sparingly.
  2. Prefer clear loop conditions over frequent breaks.

Summary

This chapter explained Kotlin control flow mechanisms including conditional statements, the when expression, loops, and flow-control keywords like break, continue, and return. Mastering these concepts is essential for building logical and efficient Kotlin programs.