Skip to main content

Kotlin Control Flow: Loops and Conditionals

In this tutorial, we'll learn about control flow in Kotlin, which includes loops and conditionals. Control flow in programming refers to the order in which individual statements, instructions or function calls are executed or evaluated. This is a fundamental concept that is necessary to understand as you learn programming. Here we'll be focusing on loops (for, while, do-while) and conditional statements (if, else, when) in Kotlin.

Loops in Kotlin

Loops are used to repeat a block of code multiple times. Kotlin provides several ways to perform looping operations: for, while, and do-while.

For Loop

The for loop iterates through anything that provides an iterator. This includes arrays, collections, or ranges of numbers.

Here's an example:

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

In this example, i is the loop variable, and 1..5 is the range. This loop will print the numbers 1 through 5.

While Loop

The while loop continues to execute a block of code as long as a certain condition is true.

Here's an example:

var i = 1
while (i <= 5) {
println(i)
i++
}

In this example, as long as i is less than or equal to 5, the loop will continue to execute. On each iteration, i is incremented by 1.

Do-While Loop

The do-while loop is similar to while, but it checks the condition after the loop body is executed. Therefore, the loop body is executed at least once.

var i = 1
do {
println(i)
i++
} while (i <= 5)

In this example, the loop will first print 1, and then increment i by 1. It will continue to do this as long as i is less than or equal to 5.

Conditional Statements in Kotlin

Conditional statements are used to perform different actions based on different conditions. Kotlin provides several ways to perform conditional logic: if, else, and when.

If Statement

The if statement executes a block of code if a specified condition is true.

Example:

var number = 10
if (number > 0) {
println("Number is positive")
}

In this example, because number is greater than 0, the message "Number is positive" is printed.

If-Else Statement

The if-else statement executes a block of code if a specified condition is true. If the condition is false, another block of code will be executed.

Example:

var number = -10
if (number > 0) {
println("Number is positive")
} else {
println("Number is not positive")
}

In this example, because number is not greater than 0, the message "Number is not positive" is printed.

When Statement

The when statement is a multi-branch statement in Kotlin. It can be used as an alternative to an if-else-if ladder with a simpler and more readable syntax.

Example:

var number = 3
when (number) {
1 -> println("One")
2 -> println("Two")
3 -> println("Three")
else -> println("Invalid number")
}

In this example, because number is 3, the message "Three" is printed.

With this, we conclude our discussion on control flow in Kotlin. Understanding these concepts is critical as you move forward in your Kotlin journey because you'll find yourself using them over and over again. Practice writing and executing these loops and conditional statements until you're comfortable with them. Happy coding!