if else in Kotlin

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 20 minutes

We make decisions, every single day, some are tiny likes what should I eat? some are huge, should I break up with her/him? Right? Depend on these tiny or huge decisions shape your life( here our programming life )

Now here is our if

if is a conditional, control flow or what so ever you read or heard, Remeber this, when you needed to make a certain decision, if comes in handy.

Here is our problem (or program)

1 you are hungry.

    var isHungry = true
    if(isHungry){
        println("Check the fridge")
    }

Here is the explaination
If I'm hungry "Check the fridge"

So how about if we are not hungry?
change the var isHungry = true to var isHungry = false
comment below

press me to the kotlin online ide

1 you are hungry.
2 check your fridge

    var isHungry = true
    var isFoodHere = true
    if(isHungry){
        println("I'm hungry") // 1
        if(isFoodHere){
            println("WHAT!! No Food!!!") // 2
        }else{
            println("Ohhh I'm dying") // 3
        }
    }else{
        println("I'm fine, I'm not hungry") // 4
    }

Here is the explaination for your hunger game.

  1. at condition one ( // 1 ) you are hungry, and it's true, so just scream
    I'm hungry
  2. at condition two ( // 2 ) you are hungry, there is food, both condition is true, so just scream
    I'm hungry
    Yay!, let's eat
  3. at condition three ( // 3 ) you are hungry, there is food, but you are not coming close to this condition, coz food is here ( isFoodHere = true), so change var isFoodHere = true to var isFoodHere = false
    comment below
    press me to the kotlin online ide
    4.at condition four ( // 4 ) you are hungry, food is here, both are true, so just forget we not gonna make this far. but let's change
    var isHungry = true to var isHungry = false
    again comment below

Did you realize that you are starting to realized if and if else, also nested if else. Take a deep breath, apperiate your effort, let's keep going.

  1. you are hungry
  2. check your fridge
  3. if there is food, eat it
  4. if not, then go to the store
  5. you found orange, check the price
  6. if lower than or equal to 10 dollor, buy 20 oranges
  7. if higher than 10 dollor, buy 10 oranges
  8. if you don't know the price, just say "orange are so expensive"

so let's start our coding journey.

    var isHungry = false
    var isFoodHere = false
    var isOrange = true
    var price = 11
    if(isHungry){
        println("Eat it")
    }else if(isFoodHere){
        println("Eat it")
    }else{
        println("No Food!!, let's go to market place")
        if(isOrange){
            println("How much for an Orange")
            if(price <= 10){
                println("Bought 20 oranges")
            }else if(price > 10){
                println("Bought 10 oranges")
            }else{
                println("Oranges are so expensive")
            }
	}
    }

Time to play!
Change isHungry, isFoodHere, isOrange, price to something your like
example var isHungry = true, var isFoodHere = true, isOrange = false, price = 0.1
comment below

<= means lower than and equal
>= means greater than and equal

now you know if, else if, else
remember this,

if(some condition){
        do this
    }else if(another condition){
        do this
    }else if(another condition again){
        do this
    }else{
        do when none of the above condition
    }

Time for short

    var name = "Jenny"
    if(name == "jenny") println("Hi I'm Jenny") else println("Who Am I?!")

above code block is equivalent to below code block, but short

    var name = "Jenny"
    if(name == "jenny"){
      println("Hi I'm Jenny")   
    } else {
     println("Who Am I?!")   
    }

Time for our expression

we can used if or if else or if else if else if else as expression.
Ok, that sounded boring, right?
Let's see the code.

    var x = 10
    var y = 20
    var max = if(x > y) x else y
    println(max)

Here the explaination
we have two variable typed Int, named x and y with respective value assigned.
check which value is greater and assign that value to another variable named max, and print it out.

Above code block is same as below code block, but short and concise. That's is one of the feature of Kotlin

    var x = 10
    var y = 20
    var max = if(x > y){
     	x   
    } else{
     	y   
    }
    println(max)

Remember this, if you used as an expression don't forget the else block, otherwise you wll see big red error (I means it's not so great).
Think about it. if this condition is not meant then we needed something else. Right?

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.