Search⌘ K
AI Features

Control Flow

Explore how Kotlin's control flow structures if and when streamline coding by returning values directly. Understand how these expressions improve readability and thread safety by avoiding multiple return statements and mutable states. This lesson helps you write concise and clear Kotlin functions using these essential constructs.

We'll cover the following...

We can say that the control flow is the bread and butter of writing programs. We’ll start with two conditional expressions, if and when.

The if expression

In Java, if is a statement. Statements do not return any value. Let’s look at the following function, which returns one of two possible values:

public String getUnixSocketPolling(boolean isBsd) {
if (isBsd) {
return "kqueue";
}
else {
return "epoll";
}
}
if statement in Java

While this example is easy to follow, in general, having multiple ...