From the course: Kotlin Essential Training: Functions, Collections, and I/O

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

if and when statements

if and when statements

- [Instructor] How do we define conditional logic in Kotlin? For this problem, we have two primary tools: if and when, both of which can be used as both a statement or an expression. An expression returns a values, whereas a statement does not. To better understand that this distinction between statement and expression, and to learn more about conditional logic, let's take a look at the basics of both if and when. If in Kotlin behaves pretty much just like any other language. We provide some Boolean expression. In this case, we could say if true and if it evaluates to true, which this always will, some line or block of code will be run. So in this case, we will print out The condition was true if this condition evaluates to true, which it will. So when we print out this code, we see the condition was true printed out to the console. Now, if statements can be written with or without curly braces. If we want to run…

Contents