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 - Kotlin Tutorial
From the course: Kotlin Essential Training: Functions, Collections, and I/O
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
-
-
-
-
-
-
(Locked)
if and when statements12m 55s
-
(Locked)
Statements vs. expressions7m 19s
-
(Locked)
try/catch as control flow6m 25s
-
(Locked)
Smart casting11m 44s
-
(Locked)
while loops5m 17s
-
(Locked)
for loops3m 49s
-
(Locked)
Challenge: Replace an if/else expression with a when expression1m 12s
-
(Locked)
Solution: Replace an if/else expression with a when expression5m 15s
-
(Locked)
-
-
-
-
-