Kotlin Academy Copyright

Kotlin — Getting Started Part 3

Here’s come Decision!

Mochamad Iqbal Dwi Cahyo
Jul 10 · 2 min read

This series was consist of the following part:

  1. Part 1
  2. Part 2
  3. This article

Yes, now it comes to decision making. Which one do you stand the light side or the dark side?

Like any programming language, the if statement is still the same but with little power added to it in Kotlin.

Decision Making

Logic statement

val age = 17
println(16 > age && 16 < age) // meh
println(!(16 > age)) // meh
println(age > 18 && age < 58) // meh
println(age in 19..57) // nice, range check (inclusive end)

When (no switch-case)

when (age) {
17 -> println(true)
else -> println(false)
} // meh
val msg = when (age) {
17 -> "true"
else -> "false"
} // hm, able to return value, same with "if"
when {
age > 17 -> println(true)
"17" == "$age" -> println(true)
else -> println(false)
} // hm, able to check for different data type of cases
when {
age in 18..57 -> println(true)
else -> println(false)
} // support range check
val msg = when (age) {
17 -> {
// support multiple line statement
"skip here, to prepare something"
"true" // return this
}
else -> "false"
} // hm, return the last statement
when (age) {
17, 18 -> "true"
else -> "false"
} // hm, support "," (or) operator inside condition cases

Okay, that’s it for today!


Happy, coding guys!!!

Easyread

Easy read, easy understanding. A good writing is a writing that can be understood in easy ways

Mochamad Iqbal Dwi Cahyo

Written by

A geek enthusiast. Sometimes photographer, culinary seeker or traveler. He ready to craft ideas!

Easyread

Easyread

Easy read, easy understanding. A good writing is a writing that can be understood in easy ways

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade