Become a Kotlin Programmer (Part 3)

Hello Guys!

Sorry for the delay, I has been some problems with my laptop, but today we will talk about the flow control using conditionals in ours programs made in Kotlin, it’s very easy the syntax for each type of conditional is very intuitive, then here we go!.

If, else statements

If you have a basic concepts of programming this section will be very easy for you, with “if” conditional you can compare the value of any variable with a hard-code value or other variable, to this type of flow is called condition, for example if i want compare the value between 2 variables, for example if the variable eggs is equal to 12 then we do something in our code, let’s see the following example.

fun main(args: Array<String>) {
    var fruit = "apple"
if (fruit == "pear"){
print("Great is a pear!")
}
    if (fruit == "apple") print("Great is an apple")
}

We can use as a expression

fun main(args: Array<String>) {
var a = "pear"
var fruit = if (fruit == "apple") "apple" else "pear"
}

And can use the classic if-else

fun main(args: Array<String>) {
    var fruit = "apple"
if (fruit == "pear"){
print("Great is a pear!")
} else {
print("=( is an apple")
}
    val min = if (x < y) x else y
}

When statement

Really Kotlin is an easy language, other of the improvements is the “when” conditional this replace the “switch” statement used in java. Now we can use in a easy and more readable form, in the next code we compare a random number in a range of number.

val random = Random().nextInt(50)
when (random) {
in 1..10 -> print("The number is between 1 to 10: $random")
in 11..20 -> print("The number is between 11 to 20: $random")
in 21..30 -> print("The number is between 21 to 30: $random")
in 31..40 -> print("The number is between 31 to 40: $random")
else -> print("The number is above 40: $random")
}

we can use a specific value to for each “when” case.

val random = Random().nextInt(50)
when (random) {
10 -> println("You have a: $random")
20 -> println("Apple minus pears is equal to: $random")
30 -> println("Oh my god!, is it your age?: $random")
40 -> println("The new twenty: $random")
else -> println("Well, good luck: $random")
}

can combined with a comma.

val random = Random().nextInt(50)
when (random) {
10, 20 -> println("You have 10 or 20 years")
30, 40 -> println("You are a middle age man")
else -> println("I don't know my friend")
}

we can use in or !in(negate in).

val random = Random().nextInt(50)
when (random) {
in 1..10 -> println("You are in the range of 1 to 10")
!in 11..15 -> println("You are a teenager")
else -> println("Well, probably you are a adult")
}

and also is or !is(negate is).

when (variable) {
is String -> variable.startsWith("Ap")
else -> println("probably is a number")
}

The other use for “when” is as if-else-if replacement if without argument conditions are simply boolean expressions, and a branch is executed when its condition is true:

when {
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}

if you want know more about Kotlin and the conditionals you can review the documentation page https://kotlinlang.org/docs/reference/control-flow.html, thanks for read this entry, Stay up, keep learning and enjoy Kotlin.

see the next entry: https://medium.com/@yosefeliyahu/become-a-kotlin-programmer-part-4-277494dcd573